Arrays can be implicitly typed by using the var designation for the type. Using var signals to the compiler that the type has not been specified and must be inferred from the assignment. The program below creates three implicitly deined arrays. The first array is just an array of strings. The second is a two-dimensional array of strings. Finally, the third is an array of objects.
using System; namespace XoaX { class Program { static void Main(string[] args) { // Implicitly defined types in a one-dimensional array var saSacramentsOfInitiation = new[] { "Baptism", "Eucharist", "Confirmation" }; Console.WriteLine("Sacraments of Initiation:"); Console.WriteLine(saSacramentsOfInitiation[0]); Console.WriteLine(saSacramentsOfInitiation[1]); Console.WriteLine(saSacramentsOfInitiation[2]); Console.WriteLine(); // Implicitly defined types in a two-dimensional array var saaParallels = new[] { new []{"Passover", "Slavery", "Promised Land"}, new []{"Last Supper", "Sin", "Heaven"}, }; Console.WriteLine("Old and New Testament Parallels:"); Console.WriteLine(saaParallels[0][0] + " and " + saaParallels[1][0]); Console.WriteLine(saaParallels[0][1] + " and " + saaParallels[1][1]); Console.WriteLine(saaParallels[0][2] + " and " + saaParallels[1][2]); Console.WriteLine(); // Implicitly-typed array with object initializers var qaApostles = new[] { new { msName = "Peter", msaScribes = new [] {"Silvanus"} }, new { msName = "Paul", msaScribes = new [] {"Tertius"} } }; Console.WriteLine("Apostles and their Scribes:"); Console.WriteLine("Apostle: " + qaApostles[0].msName + ", Scribes: " + qaApostles[0].msaScribes[0]); Console.WriteLine("Apostle: " + qaApostles[1].msName + ", Scribes: " + qaApostles[1].msaScribes[0]); Console.WriteLine(); } } }
Sacraments of Initiation: Baptism Eucharist Confirmation Old and New Testament Parallels: Passover and Last Supper Slavery and Sin Promised Land and Heaven Apostles and their Scribes: Apostle: Peter, Scribes: Silvanus Apostle: Paul, Scribes: Tertius Press any key to continue . . .
© 20072024 XoaX.net LLC. All rights reserved.