tags:

views:

326

answers:

7
+2  Q: 

Array Size in C#

How to determine size of an array in C#?

+1  A: 

yourArray.Length :)

Petar Minchev
+1  A: 

With the Length property.

int[] foo = new int[10];
int n = foo.Length; // n == 10
Joren
Thank you so much
Amal
+23  A: 

If it's a one-dimensional array a,

a.Length

will give then number of elements of a.

If b is a rectangular multi-dimensional array (for example, int[,] b = new int[3, 5];)

b.Rank

will give the number of dimensions (2), and

b.GetLength(dimensionIndex)

will get the length of any given dimension (0-based indexing for the dimensions - so b.GetLength[0] is 3 and b.GetLength[1] is 5).

See System.Array Members for more info.

As @Lucero points out in the comments, there is a concept of a "jagged array", which is really nothing more than a single-dimensional array of (typically single-dimensional) arrays. For example, one could have

int[][] c = new int[][3];
c[0] = new int[] {1, 2, 3};
c[1] = new int[] {3, 14};
c[2] = new int[] {1, 1, 2, 3, 5, 8, 13};

Note that the 3 members of c all have different lengths. In this case, as before c.Length will indicate the number of elements of c, (3) and c[0].Length, c[1].Length, and c[2].Length will be 3, 2, and 7, respectively.

Blair Conrad
For completeness you could also have listed the way jagged arrays work (e.g. `int[][]`) ;-)
Lucero
I considered it, @Lucerno, but figured it a different problem - figuring out the size of things _inside_ an array - a jagged array's really just a single-dimensioned array that happens to contain singled-dimensioned arrays, as I'm sure you know.
Blair Conrad
@Blair, of course I know that you're right and that's why I added the comment with a smiley instead of writing an answer myself. However, the distinction between `[,]` and `[][]` doesn't seem to be clear for all .NET newbies, so it may still be worth noting that `[][]` is not a multidimensional array in your answers sense.
Lucero
@Lucero, excellent point. Updated as you suggest.
Blair Conrad
+9  A: 

You can look at the documentation for Array to find out the answer to this question.

In this particular case you probably need Length:

int sizeOfArray = array.Length;

But since this is such a basic question and you no doubt have many more like this, rather than just telling you the answer I'd rather tell you how to find the answer yourself.

Visual Studio Intellisense

When you type the name of a variable and press the . key it shows you a list of all the methods, properties, events, etc. available on that object. When you highlight a member it gives you a brief description of what it does.

Press F1

If you find a method or property that might do what you want but you're not sure, you can move the cursor over it and press F1 to get help. Here you get a much more detailed description plus links to related information.

Search

The search terms size of array in C# gives many links that tells you the answer to your question and much more. One of the most important skills a programmer must learn is how to find information. It is often faster to find the answer yourself, especially if the same question has been asked before.

Use a tutorial

If you are just beginning to learn C# you will find it easier to follow a tutorial. I can recommend the C# tutorials on MSDN. If you want a book, I'd recommend Essential C#.

Stack Overflow

If you're not able to find the answer on your own, please feel free to post the question on Stack Overflow. But we appreciate it if you show that you have taken the effort to find the answer yourself first.

Mark Byers
Good job, Mark, but may be an overkill!
Nayan
@Nayan: Thanks! While it's great that the OP got his answer fast from other posters, I feel it's more important to teach beginners how to find information rather than teaching them answers to specific questions. I hope the OP finds this answer useful and that it saves them having to ask hundreds of questions that they could have found the answers to themselves.
Mark Byers
+1 for taking the time to take this approach - "give a man a fish..." etc.
Peter Kelly
If I ever have to do the same, I'll just redirect the OPs to your answer here =) And yes, I completely agree with you!
Nayan
+2  A: 

For a single dimension array, you use the Length property:

int size = theArray.Length;

For multiple dimension arrays the Length property returns the total number of items in the array. You can use the GetLength method to get the size of one of the dimensions:

int size0 = theArray.GetLength(0);
Guffa
A: 

for 1 dimensional array

int[] listItems = new int[] {2,4,8};
int length = listItems.Length;

for multidimensional array

int lenght = listItems.Rank;

To get the size of 1 dimension

int lenght =  listItems.GetLenght(0);
Johnny
+1  A: 

hey amal it goes like this: 1D:

 type[] name=new type[size]  //or =new type[]{.....elements...}

2D:

 type[][]name=new type[size][] //second brackets are emtpy

then as you use this array :

 name[i]=new type[size_of_sec.Dim]

or You can declare something like a matrix

type[ , ] name=new type [size1,size2]
Sara Ste.