get elements from a 2 dimensional array in csharp

To get elements from a 2 dimensional array in C#, you can use the indexing operator with two indices representing the row and column position of the element. Here's an example:

main.cs
int[,] numbers = new int[3, 4] {
    { 1, 2, 3, 4 },
    { 5, 6, 7, 8 },
    { 9, 10, 11, 12 }
};

int element = numbers[1, 2]; // gets the element at row 1, column 2 (i.e. value 7)
182 chars
8 lines

In this example, we have a 2 dimensional array of integers called numbers. We initialize it with 3 rows and 4 columns of values using an initialization expression. Then, we can use the indexing operator with two indices to get the element at position [1, 2], which corresponds to row 1 (the second row) and column 2 (the third column). The resulting value of element will be the integer 7.

Note that in C#, multidimensional arrays are accessed using a single indexing operator with multiple comma-separated indices, whereas jagged arrays (arrays of arrays) require nested indexing with separate indexing operators for each level of the hierarchy.

gistlibby LogSnag