none
LoweBound and UpperBound in Array ? RRS feed

  • Question

  • 1) what is the use of LoweBound and UpperBound in Array ?
        I mean when will we really need this boundires ?

    2) I saw GetUpperBound() and GetLowerBound() functions in Array class .Those functions are extern functions, I guess these functions are written in other languages. Inorder to use these functions in c# I have to use DllImport attribute right ?   but I didn't see any DllImport attribute in Array class.

    kindly help me guys..............

    Thursday, January 31, 2013 3:55 AM

Answers

  • 1) It depends on the kind of array:

    • for the usual single dimensional C# array (that is, something like int[] a = new int[10];) those 2 methods are useless, such arrays always have the lower bound equal to 0 and the upper bound is equal to a.Length - 1.
    • for multidimensional arrays (int[,] ma = new int[2,3];) GetUpperBound can be used to retrieve the upper bound of each dimension (for the above example ma.GetUpperBound(0) will return 1 and ma.GetUpperBound(1) will return 2). GetLowerBound is still useless because the lower bound of such arrays is always 0. Note that the Length property of a multidimensional array returns the total number of elements in the array, 6 for the given example.
    • while C# arrays always have 0 as lower bound it is possible to create arrays with a different lower bound by calling Array.CreateInstance: Array arr = Array.CreateInstance(typeof(int), new[] { 10 }, new[] { 1 }); This will create a single dimensional array where the lower bound is 1 instead of 0 and the upper bound is 10. For such arrays you may need to use GetLowerBound and/or GetUpperBound/Length to obtain information about the array at runtime. But such cases are probably very rare, I've never seen anyone using arrays with a non-0 lower bound. They're also more difficult to use and they're slower.

    2) DllImport is used only for PInvoke. These functions are provided by the .NET runtime itself, they don't use PInvoke.


    Thursday, January 31, 2013 6:51 AM
    Moderator

All replies

  • 1) It depends on the kind of array:

    • for the usual single dimensional C# array (that is, something like int[] a = new int[10];) those 2 methods are useless, such arrays always have the lower bound equal to 0 and the upper bound is equal to a.Length - 1.
    • for multidimensional arrays (int[,] ma = new int[2,3];) GetUpperBound can be used to retrieve the upper bound of each dimension (for the above example ma.GetUpperBound(0) will return 1 and ma.GetUpperBound(1) will return 2). GetLowerBound is still useless because the lower bound of such arrays is always 0. Note that the Length property of a multidimensional array returns the total number of elements in the array, 6 for the given example.
    • while C# arrays always have 0 as lower bound it is possible to create arrays with a different lower bound by calling Array.CreateInstance: Array arr = Array.CreateInstance(typeof(int), new[] { 10 }, new[] { 1 }); This will create a single dimensional array where the lower bound is 1 instead of 0 and the upper bound is 10. For such arrays you may need to use GetLowerBound and/or GetUpperBound/Length to obtain information about the array at runtime. But such cases are probably very rare, I've never seen anyone using arrays with a non-0 lower bound. They're also more difficult to use and they're slower.

    2) DllImport is used only for PInvoke. These functions are provided by the .NET runtime itself, they don't use PInvoke.


    Thursday, January 31, 2013 6:51 AM
    Moderator
  • You will need the bounds when the arrays are created by some external code (VB6, ActiveX, etc.). For example this discussion deals with Excel arrays, where lower bound is not always zero: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/82248560-dabd-4c90-b1e2-793b2f32b257.


    • Edited by Viorel_MVP Thursday, January 31, 2013 8:33 AM
    Thursday, January 31, 2013 8:31 AM
  • you are the right person for this question. Thanks a lot dude.

    I didn't get your answer for second one.

    what you mean PInvoke() ?????

    little bit of struggling in first one.

    "for multidimensional arrays (int[,] ma = new int[2,3];) GetUpperBound can be used to retrieve the upper bound of each dimension (for the above example ma.GetUpperBound(0) will return 1 and ma.GetUpperBound(1) will return 2). GetLowerBound is still useless because the lower bound of such arrays is always 0. Note that the Length property of a multidimensional array returns the total number of elements in the array, 6 for the given example."

    how come ma.GetUpperBound(0) will return 1 and GetUpperBound(1) will return 2 could you please clearly tell this one?

    Thursday, January 31, 2013 9:19 AM
  • "what you mean PInvoke() ?????"

    PInvoke is what allows you to call unmanaged, native code. Like the Win32 API. http://msdn.microsoft.com/en-us/library/aa288468(v=VS.71).aspx. Since you mentioned DllImport you probably know already that this is possible but you didn't know how it is called.

    "how come ma.GetUpperBound(0) will return 1 and GetUpperBound(1) will return 2 could you please clearly tell this one?"

    So, the array "new int[2,3]" has 2 dimensions. The first dimension's length is 2 (in the bidimensional case you can say that this array has 2 rows) and the second's dimension length is 3 (you can that this array has 3 columns). For the first dimension valid indexes are 0 and 1 so the upper bound of dimension 0 is 1. For the second dimension valid indexes are 0,1 and 2 so the upper bound of dimension 1 is 2. It should be easy to see all this in a table:

    [0, 0]  [0, 1]  [0, 2]

    [1, 0]  [1, 1]  [1, 2]

    Thursday, January 31, 2013 11:42 AM
    Moderator