• Difference between array.GetLength(0) and array.GetUpperBound(0)


    Difference between array.GetLength(0) and array.GetUpperBound(0)

    What is the difference between these two methods and when would you use one instead of the other?

    int[,] array = new int[4,3];
    int length0 = array.GetLength(0);
    int upperbound0 = array.GetUpperBound(0);

    MSDN says that GetLength return the number of elements where as GetUpperBound determine the max index, but how could this be different since arrays are initialized with elements for each index?

    回答1

    Take a look at this (rarely used) method. From Docs:

    public static Array CreateInstance(Type elementType, int[] lengths, int[] lowerBounds)

    Creates a multidimensional Array of the specified Type and dimension lengths, with the specified lower bounds.

    With it, you can create an array with indices from -5 ... +5. If you ever use this kind of array, then GetUpperBound() suddenly becomes a lot more useful than GetLength()-1. There also exists a GetLowerBound().

    But the C# support for this kind of arrays is low, you cannot use []. You would only need those methods in combination with the Array.GetValue() and SetValue() methods.

    回答2

    I realise this is an old question but I think it's worth emphasising that GetUpperBound returns the upper boundary of the specified dimension. This is important for a multidimensional array as in that case the two functions are not equivalent.

    // Given a simple two dimensional array
    private static readonly int[,] USHolidays =
    {
        { 1, 1 },
        { 7, 4 },
        { 12, 24 },
        { 12, 25 }
    };

    The Length property will output 8 as there are 8 elements in the array.

    Console.WriteLine(USHolidays.Length);

    However, the GetUpperBound() function will output 3 as the upper boundary of the first dimension is 3. In other words I can loop over array indexes 0, 1, 2 and 3.

    Console.WriteLine(USHolidays.GetUpperBound(0));
    for (var i = 0; i <= USHolidays.GetUpperBound(0); i++)
    {
        Console.WriteLine("{0}, {1}", USHolidays[i, 0], USHolidays[i, 1]);
    }

    Array.GetUpperBound(Int32) Method

    Definition

    Gets the index of the last element of the specified dimension in the array.

    C#
    public int GetUpperBound (int dimension);

    Parameters

    dimension
    Int32

    A zero-based dimension of the array whose upper bound needs to be determined.

    Returns

    Int32

    The index of the last element of the specified dimension in the array, or -1 if the specified dimension is empty.

    Exceptions

    dimension is less than zero.

    -or-

    dimension is equal to or greater than Rank.

    Remarks

    GetUpperBound(0) returns the last index in the first dimension of the array, and GetUpperBound(Rank - 1) returns the last index of the last dimension of the array.

    This method is an O(1) operation.

  • 相关阅读:
    我的软件工程课目标
    软件工程课程的建议
    结对编程学习fault、error、failure三种状态
    结对编程项目的过程记录与收获
    “结对编程”之我见
    关于问卷调查
    我的软件工程课目标
    软件工程课程建议
    结对编程2
    结对编程之四则运算
  • 原文地址:https://www.cnblogs.com/chucklu/p/16772748.html
Copyright © 2020-2023  润新知