What is the purpose of Array.GetLowerBound(int)?

前端 未结 3 922
逝去的感伤
逝去的感伤 2021-02-19 10:01

I don\'t understand the purpose of Array.GetLowerBound().

Does it ever return non-zero? When? How?

Thanks.

相关标签:
3条回答
  • 2021-02-19 10:18

    On a multi dimensional array in VB or various COM derived APIs you can query the lower bound by dimension. Array types can be 0 or 1 based (i.e. starting from zero or 1) and this applies to multidimensional arrays as well.

    This can also apply to arrays exposed through COM interop. For example, many Excel APIs use 1-based arrays and many APIs functions use variant arrays as parameters (the variant was essentially invented as a data type for a spreadsheet cell).

    When using COM interop you still have to play nicely with these APIs and type systems. They were originally designed to be used with VBA, and the 'classic' VB4-6 language variants had a truly baroque type system due to their tight coupling with COM. The .Net type systems of C# et. al. are somewhat less painful than their COM-based predecessors, but you still get to feel the pain when using COM interop.

    0 讨论(0)
  • 2021-02-19 10:23

    Theoretically, you can create arrays with any lower or upper bound for indexing. VB.NET can use this to make arrays with a lower bound of 1 in order to be compatible with some older VB verions, but you can actually use Array.CreateInstance(Type,Int32[],Int32[]) to create an array with any lower bound you wish.

    0 讨论(0)
  • 2021-02-19 10:32

    There is one CreateInstance call that sets non-zero lower bounds:

    CreateInstance(Type, Int32[], Int32[])
    Creates a multidimensional Array of the specified Type and dimension lengths, with the specified lower bounds.

    Compare this to:

    CreateInstance(Type, Int32[])
    Creates a multidimensional Array of the specified Type and dimension lengths, with zero-based indexing. The dimension lengths are specified in an array of 32-bit integers.

    (Or to any of the other overloads, in fact. Of the six CreateInstance overloads, five create a zero-based array.)

    0 讨论(0)
提交回复
热议问题