Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

What's the difference between "Length", "Count()" and "Rank" for a .NET array?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
318 views
Welcome To Ask or Share your Answers For Others

1 Answer

Length is the property of an array object and using it is the most effective way to determine the count of elements in the array (Array.Length in MSDN documentation).

Count() is a LINQ extension method that does effectively the same. It applies to arrays because arrays are enumerable objects. It's preferred to use Length, because Count() is likely to be more expensive (see this question for further discussion and MSDN documentation on Count for reference).

Rank is the property that returns the number of dimensions (a different thing entirely). When you declare an array int[,] myArray = new int[5,10];, the Rank of it will be 2, but it will hold a total of 50 elements (MSDN on Rank property).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...