Answers for "c# two dimensional array"

C#
32

c# two dimensional array

// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified.
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// A similar array with string elements.
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
                                        { "five", "six" } };

// Three-dimensional array.
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                 { { 7, 8, 9 }, { 10, 11, 12 } } };
// The same array with dimensions specified.
int[, ,] array3Da = new int[2, 2, 3] 
{ { { 1, 2, 3 }, { 4, 5, 6 } },{ { 7, 8, 9 }, { 10, 11, 12 } } };
Posted by: Guest on March-10-2020
3

how to use a 2d array in csharp

int[,] arr = new int[3,3];//declaration of 2D array  
int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }}//another way to declare
arr[1,2]=20; //access to the array
Posted by: Guest on October-18-2020
1

multidimensional array c#

// The following are multidimensional arrays
// Two-dimensional array. (4 rows x 2 cols)
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// Three-dimensional array. (4 rows x 2 cols)
int[,,] array3D = new int[,,] { 
  									 { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } },
                                     { { 13, 14, 15, 16 }, { 17, 18, 19, 20 }, { 21, 22, 23, 24 } } 
                                   };
int[,,] array3D = new int[2, 3, 4] { 
  									 { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } },
                                     { { 13, 14, 15, 16 }, { 17, 18, 19, 20 }, { 21, 22, 23, 24 } } 
                                   };

// C# also has jagged arrays

// A "multidimensional array" is essentially a matrix occupying one block of memory
// The "jagged array" is an array of arrays 
//  - with no restriction that the latter arrays need to be of the same dimension/length
//  - each of these latter arrays also occupy their own block in memory

int[][] jArray = new int[3][]{
  					new int[2]{1, 2},
					new int[3]{3, 4, 5},
                	new int[4]{6, 7, 8, 9}
            	};

jArray[1][2]; //returns 4

// We can change a whole element of array
jArray[1] = new int[3] { 10, 11, 12 }; 

jArray[1][2]; //returns 11

int[][][] intJaggedArray = new int[2][][] 
                            {
                                new int[2][]  
                                { 
                                    new int[3] { 1, 2 },
                                    new int[2] { 4, 5, 6 } 
                                },
                                new int[1][]
                                { 
                                    new int[3] { 7, 8, 9, 10 }
                                }
                            };

intJaggedArray[0][1][2]; //returns 6

// https://stackoverflow.com/a/4648953/9034699
// https://www.tutorialsteacher.com/csharp/csharp-jagged-array
Posted by: Guest on June-01-2021
1

bidimensional array c#

int[,] array5;
array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };   // OK
//array5 = {{1,2}, {3,4}, {5,6}, {7,8}};   // Error
Posted by: Guest on January-27-2021
0

initialize matrix c#

int[,] matrix = new int[10,9];
Posted by: Guest on February-01-2020

Code answers related to "c# two dimensional array"

C# Answers by Framework

Browse Popular Code Answers by Language