.net array - difference between “Length”, “Count()” and “Rank”

前端 未结 6 2313
时光说笑
时光说笑 2020-12-15 15:19

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

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 16:08

    Length is the property of 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 (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).

    EDIT: thanks to Kornelije Petak for relevant MSDN links.

提交回复
热议问题