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

0

6 Answers 6

1

you can do it like this. See details here

int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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

Comments

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

1 Comment

You are right with the link. I deleted the comment.
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.