Arnis Lapsa
Arnis Lapsa

Reputation: 47677

Get dimension length, c# arrays

int[,] arr = new int[2,5];
var rows = arr.?
var cols = arr.?

Assert.Equals(3, rows);
Assert.Equals(6, cols);

Upvotes: 3

Views: 3833

Answers (5)

duraz0rz
duraz0rz

Reputation: 397

var rows = arr.GetLength(0);
var cols = arr.GetLength(1);

Upvotes: 0

Lasse Espeholt
Lasse Espeholt

Reputation: 17792

You can use GetLength(some-dimension-starting-from-0) on a array.

var rows = arr.GetLength(0);
var cols = arr.GetLength(1);

But rows will be 2 and columns 5.

var arr = new int[2,3]

Will give you:

arr[0,0]
arr[0,1]
arr[0,2]
arr[1,0]
arr[1,1]
arr[1,2]

Upvotes: 6

thelost
thelost

Reputation: 6694

arr.GetLength(index)

Upvotes: 3

Elisha
Elisha

Reputation: 23800

You can use GetLength() method of array that let you know what is the length of each dimension.

var rows = arr.GetLength(0);
var columns = arr.GetLength(1);

Just to make clear, it gets the size, so in your example rows will be 2.

Upvotes: 6

Kelsey
Kelsey

Reputation: 47776

arr.GetLength(dimensionYouWant);

Upvotes: 3

Related Questions