How can I start the index in an ArrayList
at 1 instead of 0? Is there a way to do that directly in code?
(Note that I am asking for ArrayList
One legitimate reason to do this is in writing unit tests. Certain 3rd party SDKs (for example Excel COM interop) expect you to work with arrays that have one-based indices. If you're working with such an SDK, you can and should stub out this functionality in your C# test framework.
The following should work:
object[,] comInteropArray = Array.CreateInstance(
typeof(object),
new int[] { 3, 5 },
new int[] { 1, 1 }) as object[,];
System.Diagnostics.Debug.Assert(
1 == comInteropArray.GetLowerBound(0),
"Does it look like a COM interop array?");
System.Diagnostics.Debug.Assert(
1 == comInteropArray.GetLowerBound(1),
"Does it still look like a COM interop array?");
I don't know if it's possible to cast this as a standard C# style array. Probably not. I also don't know of clean way to hard-code the initial values of such an array, other than looping to copy them from a standard C# array with hard-coded initial values. Neither of these shortcomings should be a big deal in test code.