2d array in c# code example

Example 1: 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 } } };

Example 2: 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

Example 3: how to make a n*n 2d dynamic array in c++

int** a = new int*[rowCount];
for(int i = 0; i < rowCount; ++i)
    a[i] = new int[colCount];

Example 4: how to declare 2d array in c#

// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

Example 5: C# public 2d array

// in namespace, above main form declaration

public class Globals
    {
        public static string[,] tableArray;
	}

//... in main or other method

Globals.tableArray = new string[rowLength,colLength];

Tags: