0

I am at beginner level of programming, and I would like to know how to declare a 2-dimension array in C#. I did look it up on Google but I couldn't find a solution.

Please answer this question at my level.

Thanks

Tonio
  • 414
  • 5
  • 17
Faiq
  • 5
  • 3

6 Answers6

1

you can do it like this. See details here

int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
Ehsan
  • 31,833
  • 6
  • 56
  • 65
1

2D integer array

Declaration

int[,] array = new int[4, 2];

Initialization

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

Complete explanation with example :http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

0

i think you have not searched google....

follow below link to see tutorial http://www.tutorialspoint.com/csharp/csharp_multi_dimensional_arrays.htm

int [,] a = int [3,4] = {  
 {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
 {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
 {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
};
Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73
0

Use MSDN for Micrsoft technologies, it is well documented http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

It ranked #1 in google search for me when writing: 2d integer array c#

This page might also provide useful information: What are the differences between a multidimensional array and an array of arrays in C#?

Community
  • 1
  • 1
Tonio
  • 414
  • 5
  • 17
0

// 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 } } };
Jackcob
  • 317
  • 2
  • 15
0
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

The sum2D() Method

private double sum2D(double[,] ar)
{
 double sum = 0.0;

 foreach (double d in ar)
      sum += d;

 return sum;
}
Nayeem Mansoori
  • 821
  • 1
  • 15
  • 41