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

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

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

6条回答
  •  一整个雨季
    2020-12-15 16:17

    • Length: Total number of elements in an array
    • LongLength: Same as Length, but returned as long (in case it's >= 231)
    • Count(): LINQ extension method that works with other collection types as well
    • Rank: Number of dimensions in array (always 1 for vectors). Only in .NET 3.5+.
    • GetLength(), GetLongLength(): Length of a certain dimension of an array
    • GetLowerBound(): Starting index of a certain dimension of an array; always 0 for vectors
    • GetUpperBound(): Ending index of a certain dimension of an array; always Length - 1 for vectors

    Interestingly, there's no GetLongUpperBound() or GetLongLowerBound()...


    Now that we're on the topic, what is the difference between an array and a vector in .NET?

    Arrays versus Vectors

    Vectors are what you normally call "1D" arrays in C#. However, 1-dimensional arrays are actually not of a type like int[], but they're of the type int[*]. C# doesn't directly support them; however, they can be created with Array.CreateInstance, and can have non-zero lower bounds. They are, however, slightly slower than vectors, because vectors are directly supported in the CLR. Because 1-dimensional arrays are actually rarely used, C# has decided not to support them (although it can use them through the use of the var keyword, from another module which declares them!).

提交回复
热议问题