Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Bug with Array.CreateInstance() when lower bound > 0

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gianluca - 18 Nov 2006 19:29 GMT
If you create an array using Array.CreateInstance() and use a lower bound >
0, you apparently get an array of the wrong type.

int[] lenghts = new int[] {1};
int[] lowerBounds = new int[] {1};
string[] ar = (string[])Array.CreateInstance(typeof(string), lengths,
lowerBounds);

Throws an InvalidCastException because "string[*]" cannot be casted to
"string[]".

int[] lenghts = new int[] {1};
int[] lowerBounds = new int[] {0};
string[] ar = (string[])Array.CreateInstance(typeof(string), lengths,
lowerBounds);

This works fine. The only difference in the lower bound.

int[] lenghts = new int[] {1,1};
int[] lowerBounds = new int[] {1,1};
string[,] ar = (string[,])Array.CreateInstance(typeof(string), lengths,
lowerBounds);

Even this work fine. The difference is the number of dimensions. So the
problem cannot be just the lower bound > 0 because in this case (and with
more dimensions too) it works well.

Everything works well in .NET 1.1. So it's apparently a bug introduced in
.NET 2.0.

Has anyone seen this before? And what is "string[*]"?

Regards,
Gianluca
Jon Skeet [C# MVP] - 19 Nov 2006 08:39 GMT
> If you create an array using Array.CreateInstance() and use a lower bound >
> 0, you apparently get an array of the wrong type.

Well, sort of.

Internally, .NET has two types of array - "vectors" (single-
dimensional, 0-based) and more general arrays.

When C# uses a single-dimensional array, it *assumes* it's one of these
"vector" types and that's what it really casts it to.

If you need to use a single-dimensional array with a non-zero lower
bound in C#, you need to access it using the Array class (and IList
etc).

> Everything works well in .NET 1.1. So it's apparently a bug introduced in
> .NET 2.0.

Hmm... I thought I'd seen exactly the same behaviour in 1.1...
unfortunately I can't easily test it any more.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Gianluca - 19 Nov 2006 21:24 GMT
Yep. I know about the internal difference between the vector and the array.
And you are right that it doesn't work with .NET 1.1 as well. I was testing
MD arrays and it also works if you cast it in the immediate window.

> > If you create an array using Array.CreateInstance() and use a lower bound >
> > 0, you apparently get an array of the wrong type.
[quoted text clipped - 16 lines]
> Hmm... I thought I'd seen exactly the same behaviour in 1.1...
> unfortunately I can't easily test it any more.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.