How can I determine size (length/Number of Objects) in an array in C#?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
for 1 dimensional array
for multidimensional array
To get the size of 1 dimension
it goes like this: 1D:
2D:
then as you use this array :
or You can declare something like a matrix
You can look at the documentation for Array to find out the answer to this question.
In this particular case you probably need 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:For multiple dimension arrays the
Length
property returns the total number of items in the array. You can use theGetLength
method to get the size of one of the dimensions:In most of the general cases 'Length' and 'Count' are used.
Array:
Typed List Array:
If it's a one-dimensional array
a
,will give the number of elements of
a
.If
b
is a rectangular multi-dimensional array (for example,int[,] b = new int[3, 5];
)will give the number of dimensions (2) and
will get the length of any given dimension (0-based indexing for the dimensions - so
b.GetLength(0)
is 3 andb.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:
Note that the 3 members of
c
all have different lengths. In this case, as beforec.Length
will indicate the number of elements ofc
, (3) andc[0].Length
,c[1].Length
, andc[2].Length
will be 3, 2, and 7, respectively.