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.

1
  • 1
    FYI, the ArrayList class is deprecated as of .NET 2.0. It should not be used. Instead, List<object> should be used if the list needs to contain objects of arbitrary types. Commented Jan 19, 2010 at 13:53

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@Jon Skeet - nice to see you again on a question i answered aswell ;)
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: 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.
3

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"

3 Comments

Is that still true in Visual Basic.NET? (I'm not a VB programmer) support.microsoft.com/kb/311333
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.