简体   繁体   中英

How to declare 2D integer array in C#?

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

you can do it like this. See details here

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

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

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 */
};

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#?

// 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 } } };
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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM