GetUpperBound() and GetLowerBound() function for array

前端 未结 3 1084
无人共我
无人共我 2021-02-05 22:32

Can anyone please tell what does the two functions do? They take an integer argument which is told to be dimension. But how does the value of this integer changes the output?

3条回答
  •  礼貌的吻别
    2021-02-05 23:00

    Can anyone please tell what does the two functions do?

    It is written in their MSDN pages. They gets the index of the first / last element of the specified dimension in the array. Take a look Array.GetUpperBound and Array.GetLowerBound

    They take an integer argument which is told to be dimension.

    Yes, as Patashu mentioned, arrays can have multidimension.

    Any idea why GetLowerBound() is always returning 0? If this always returns 0 then why do we need to call this method?

    In an array, every dimension can have their specific lower and uppor bounds. So, this methods can create different results for each dimension of array.

    Note that, although most arrays in the .NET Framework are zero-based (that is, the GetLowerBound method returns zero for each dimension of an array), the .NET Framework does support arrays that are not zero-based. Such arrays can be created with the CreateInstance(Type, Int32\[\], Int32\[\]) method, and can also be returned from unmanaged code.

    Check out;

    • Workaround: Arrays with non-zero lower bounds
    • How to: Specify a Zero Lower Bound on an Array

提交回复
热议问题