• Top Posts

    Multi Dimensional array in c#


    • Example to create C# multi dimensional array in c# and return those values.



      // Main Method   
         public static void Main()  
         {  
           // Two-dimensional array   
           int[,] intarray = new int[,] { { 1, 2 },  
                          { 3, 4 },  
                          { 5, 6 },  
                          { 7, 8 } };  
           // The same array with dimensions   
           // specified 4 row and 2 column.   
           int[,] intarray_d = new int[4, 2] { { 1, 2 }, { 3, 4 },  
                            { 5, 6 }, { 7, 8 } };  
           // A similar array with string elements.   
           string[,] str = new string[4, 2] { { "one", "two" },  
                           { "three", "four" },  
                           { "five", "six" },  
                           { "seven", "eight" } };  
           // Three-dimensional array.   
           int[,,] intarray3D = new int[,,] { { { 1, 2, 3 },  
                            { 4, 5, 6 } },  
                            { { 7, 8, 9 },  
                           { 10, 11, 12 } } };  
           // The same array with dimensions   
           // specified 2, 2 and 3.   
           int[,,] intarray3Dd = new int[2, 2, 3] { { { 1, 2, 3 },  
                              { 4, 5, 6 } },  
                              { { 7, 8, 9 },  
                             { 10, 11, 12 } } };  
           // Accessing array elements.   
           Console.WriteLine("2DArray[0][0] : " + intarray[0, 0]);  
           Console.WriteLine("2DArray[0][1] : " + intarray[0, 1]);  
           Console.WriteLine("2DArray[1][1] : " + intarray[1, 1]);  
           Console.WriteLine("2DArray[2][0] " + intarray[2, 0]);  
           Console.WriteLine("2DArray[1][1] (other) : "  
                        + intarray_d[1, 1]);  
           Console.WriteLine("2DArray[1][0] (other)"  
                      + intarray_d[1, 0]);  
           Console.WriteLine("3DArray[1][0][1] : "  
                     + intarray3D[1, 0, 1]);  
           Console.WriteLine("3DArray[1][1][2] : "  
                    + intarray3D[1, 1, 2]);  
           Console.WriteLine("3DArray[0][1][1] (other): "  
                      + intarray3Dd[0, 1, 1]);  
           Console.WriteLine("3DArray[1][0][2] (other): "  
                      + intarray3Dd[1, 0, 2]);  
           // using nested loop show string elements   
           Console.WriteLine("To String element");  
           for (int i = 0; i < 4; i++)  
             for (int j = 0; j < 2; j++)  
               Console.Write(str[i, j] + " ");  
           Console.ReadLine();  
         }  
       }  
    

    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728