Quantcast
Channel: .Net arrays with lower bound > 0 - Stack Overflow
Viewing all articles
Browse latest Browse all 6

.Net arrays with lower bound > 0

$
0
0

Although perhaps a bizare thing to want to do, I need to create an Array in .Net with a lower bound > 0. This at first seems to be possible, using:

Array.CreateInstance(typeof(Object), new int[] {2}, new int[] {9});

Produces the desired results (an array of objects with a lower bound set to 9). However the created array instance can no longer be passed to other methods expecting Object[] giving me an error saying that:

System.Object[*] can not be cast into a System.Object[]. What is this difference in array types and how can I overcome this?

Edit: test code =

Object x = Array.CreateInstance(typeof(Object), new int[] {2}, new int[] {9});Object[] y = (Object[])x;

Which fails with: "Unable to cast object of type 'System.Object[*]' to type 'System.Object[]'."

I would also like to note that this approach DOES work when using multiple dimensions:

Object x = Array.CreateInstance(typeof(Object), new int[] {2,2}, new int[] {9,9});Object[,] y = (Object[,])x;

Which works fine.


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles



Latest Images