[c#] Array Size (Length) in C#

How can I determine size of an array (length / number of items) in C#?

This question is related to c# arrays size

The answer is


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);

yourArray.Length :)


In most of the general cases 'Length' and 'Count' are used.

Array:

int[] myArray = new int[size];
int noOfElements = myArray.Length;

Typed List Array:

List <int> myArray = new List<int>();
int noOfElements = myArray.Count;

What has been missed so far is what I suddenly was irritated about:

How do I know the amount of items inside the array? Is .Length equal .Count of a List?

The answer is: the amount of items of type X which have been put into an array of type X created with new X[number] you have to carry yourself!

Eg. using a counter: int countItemsInArray = 0 and countItemsInArray++ for every assignment to your array.

(The array just created with new X[number] has all space for number items (references) of type X already allocated, you can assign to any place inside as your first assignment, for example (if number = 100 and the variable name = a) a[50] = new X();.

I don't know whether C# specifies the initial value of each place inside an array upon creation, if it doesn't or the initial value you cannot compare to (because it might be a value you yourself have put into the array), you would have to track which places inside the array you already assigned to too if you don't assign sequentially starting from 0 (in which case all places smaller than countItemsInArray would be assigned to).)

In your question size of an array (length / number of items) depending on whether / is meant to stand for "alternative" or "divide by" the latter still has to be covered (the "number of items" I just gave as "amount of items" and others gave .Length which corresponds to the value of number in my code above):

C# has a sizeof operator (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/sizeof). It's safe to use for built-in types (such as int) (and only operates on types (not variables)). Thus the size of an array b of type int in bytes would be b.Length * sizeof(int).

(Due to all space of an array already being allocated on creation, like mentioned above, and sizeof only working on types, no code like sizeof(variable)/sizeof(type) would work or yield the amount of items without tracking.)


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 1 dimensional array

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

for multidimensional array

int length = listItems.Rank;

To get the size of 1 dimension

int length =  listItems.GetLength(0);

With the Length property.

int[] foo = new int[10];
int n = foo.Length; // n == 10

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]

If it's a one-dimensional array a,

a.Length

will give the 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 documentation 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 the following:

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.