array indexes

  • Thread starter Thread starter Bharat Gupta
  • Start date Start date
B

Bharat Gupta

Can i give my own dimensions to the index of an array e.g. i can create
an array like
a[10]
a[11]
..
..
..
..
a[20]

Like i want the array index to start from 10.
 
Bharat Gupta said:
Can i give my own dimensions to the index of an array e.g. i can create
an array like
a[10]
a[11]
.
.
.
.
a[20]

Like i want the array index to start from 10.

Sort of. You can create an instance of System.Array with a different
lower bound. However, you then can't cast that to an array in C# terms
- e.g. Foo[] x. However again, you *can* cast it to IList instead.
Here's what I mean:

using System;
using System.Collections;

public class Test
{
static void Main()
{
// Create an int array which runs from 10 to 14.
Array array = Array.CreateInstance
(typeof(int), new int[] {5}, new int[]{10});

// This would fail
// int[] array2 = (int[])array;

IList list = array;

// This would fail
// list[5] = 10;
list[10] = 20;
}
}

Note that the above involves boxing, which is somewhat unfortunate, and
doesn't provide compile-time type safety.

Basically, it's doable but unpleasant.
 
Back
Top