views:

290

answers:

2

With reference to MSDN, It is stated that "You can set the lower bound of an Array, but the lower bound of an ArrayList is always zero"

If i declare an array a[10], the lower bound is always a[0].

Is this the lower bound specified there? If yes, How can we set the lower bound of an array, Since the index of an array always starts with a[0].

Or is the lower bound stated in the link is something different?

Note: I know the link point to the contents of .NET Framework 1.1 but still curious to know what exactly they have mentioned.

+3  A: 

The lower bound in C# and VB.NET is always at 0. Visual Basic 6.0 and older allowed for variable lower bounds. They removed it for the rewriting of the language for .NET.

Here is an article that goes into detail of how to do it: http://msdn.microsoft.com/en-us/magazine/cc301755.aspx. Look for "Creating Arrays with a Non-zero Lower Bound"

Daniel A. White
Is that still true in Visual Basic.NET? (I'm not a VB programmer)http://support.microsoft.com/kb/311333
Andrew Barrett
@Andrew I guess not - http://msdn.microsoft.com/en-us/library/23s0z8fy%28VS.71%29.aspx
Daniel A. White
I believe that VB silently subtracts one from any array access expression. It doesn't actually create 1-based arrays as far as the CLR is concerned.
Jon Skeet
+3  A: 

You can create an array with a non-zero lowerbound using Array.CreateInstance.

Note that you won't be able to cast that to a Foo[] (where Foo is the relevant type) unless you also make it multidimensional. There are two types of array inside the CLR - a vector (zero based, single dimensional) and an array (can be multi-dimensional and have non-zero lower bound).

A T[] in C# always corresponds to a vector, whereas a T[][] corresponds to an array. So you can do:

int[][] rectangle = (int[][]) Array.CreateInstance(typeof(int),
                                       new int[]{2, 2}, // lengths
                                       new int[]{-1, -1}); // lower bounds

but this will fail:

int[] rectangle = (int[]) Array.CreateInstance(typeof(int),
                                       new int[]{2}, // length
                                       new int[]{-1}); // lower bound

Likewise you can't cast it to IEnumerable<int> or IList<int> - although you can iterate over it with IEnumerable just fine.

Personally I would avoid using non-zero lower-bounded arrays like the plague. They're slow, and painful to work with.

Jon Skeet
@Jon Skeet - nice to see you again on a question i answered aswell ;)
Daniel A. White
If i am wrong please clear me, as the answer states, a single dimension array is a vector always? yet size of the vector can be varied as like arraylist which isn't possible with array.
Sri Kumar
@Sri Kumar: No, look at the second example - it's a single dimensional array, but it's not a vector. Note that this is "vector" in the CLR sense, not a class called Vector like in Java. And no, the size of a vector can't change.
Jon Skeet
Thanks Jon Skeet! thanks for the "No"!
Sri Kumar