Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Initialize a two-dimensional rectangular array, and use the array properties and methods

/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /*   Example10_7.cs illustrates how to initialize   a two-dimensional rectangular array, and use the   array properties and methods */ using System; public class Example10_7 {   public static void Main()   {     // create and initialize the names array     string[,] names =     {       {"Jason", "Marcus", "Price"},       {"Steve", "Edward", "Smith"},       {"Cynthia", "Ann", "Williams"},       {"Gail", "Patricia", "Jones"},     };     // display the Rank and Length properties of the names array     Console.WriteLine("names.Rank (number of dimensions) = " + names.Rank);     Console.WriteLine("names.Length (number of elements) = " + names.Length);     // use the GetLength() method to get number of elements     // in each dimension of the names array     int numberOfRows = names.GetLength(0);     int numberOfColumns = names.GetLength(1);     Console.WriteLine("Number of rows = " + numberOfRows);     Console.WriteLine("Number of columns = " + numberOfColumns);     // display the elements of the names array     for (int row = 0; row < numberOfRows; row++)     {       for (int column = 0; column < numberOfColumns; column++)       {         Console.WriteLine("names[" + row + ", " + column + "] = " +           names[row, column]);       }     }   } }