Creating a non 0 based array
I need to create an Array where the first element is not 0. I know it can be done but I can't find anything that tells me how.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
GordonPosted Dec 23, 2008, 6:24 AM
Jan MontanoPosted Dec 22, 2008, 11:24 PM
The only way I know of creating a non zero base index array is by using Array.CreateInstance
// create a 1 dimensional array of type int with 5 elements with a starting index of 1.
Array myArray = Array.CreateInstance(typeof(int), new int[1] { 5 }, new int[1] { 1 });
// store 10 to the array with index 1.
myArray.SetValue(10, 1);
// store 9 to the array with index 0. this will produce an error because our staring index is 1.
myArray.SetValue(9,0);