Basic Usage Example of C Two Dimensional Array

  • 2021-08-17 00:52:57
  • OfStack

This article illustrates the basic usage of C # 2-dimensional arrays. Share it for your reference, as follows:


// Defining Array  
string[,] classes = new string[5, 2]; 
// Correct C#2 How to use dimensional arrays  
classes[i, 0] = ""; 
// Wrong usage  
classes[i][0]=""; 

It is said that this form of C # 2-dimensional array is called sawtooth array.

1 example for reference:


//  Declaration 1 A serrated array with two elements  
int[][] myArray = new int[2][];  
//  Among them 1 Elements are 1 Contains 5 An array of three elements  
//  Initialization myArray[0] 
myArray[0] = new int[5] {1,3,5,7,9}; 
//  Among them 2 Elements are 1 Contains 4 An array of three elements  
//  Initialization myArray[1] 
myArray[1] = new int[4] {0, 2, 4, 6}; 
//  Per 1 Print out all the elements in the array of the array  
for (int i=0; i < myArray.Length; i++)  
{ 
 Console.Write(" No. 1 ({0}) Array of numbers : ", i); 
 //  Print 1 Elements in Dimensional Array  
 for (int j=0; j < myArray[i].Length; j++) 
 { 
   Console.Write("{0} ", myArray[i][j]); 
 } 
 Console.WriteLine(); 
}

I hope this article is helpful to everyone's C # programming.


Related articles: