English 中文(简体)
ArrayList and Array difference
原标题:

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.

最佳回答

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.

问题回答

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"





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签