How to determine size of an array in C#?
With the Length
property.
int[] foo = new int[10];
int n = foo.Length; // n == 10
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.
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.
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);
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);
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]