0

How do I access a row of a 2-dimensional array in C#? I want to get the count of row i.

Thanks!

2
  • This is a good resource: > http://www.dotnetperls.com/2d-array Commented Feb 18, 2012 at 17:31
  • I know this is old, but the link has changed. I guess this is why you don't just post a url. dotnetperls.com/2d Commented Feb 22, 2022 at 16:21

2 Answers 2

7

The question really depends on what type of 2D array you are thinking about and what dimension your row is.

Proper 2D array

// Init
var arr = new int[5,10];

// Counts
arr.GetLength(0) // Length of first dimension: 5
arr.GetLength(1) // Length of second dimension: 10

Jagged "2D" array

// Init
var arr = new int[3][];
// Initially arr[0],arr[1],arr[2] is null, so we have to intialize them:
arr[0] = new int[5];
arr[1] = new int[4];
arr[2] = new int[2];

// Counts
arr.Length // Length of first dimension
// In this case the "second dimension" (if you can call it that) is of variable size
arr[0].Length // Length: 5
arr[1].Length // Length: 4
arr[2].Length // Length: 2
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, 2d arrays are a lot different in C#
0

This will also work,

string[,] a = new string[,]
{
    {"ant", "aunt"},
    {"Sam", "Samantha"},
    {"clozapine", "quetiapine"},
    {"flomax", "volmax"},
    {"toradol", "tramadol"}
};

// a[1,1] is equal to Samantha

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.