C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,556 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am trying to create an single diminsion array that starts from 1 and ends at 7.
I tried this:
Int32[] lowerBounds = {1};
Int32[] lengths = {7};
Int32[] Julia = (Int32[])
Array.CreateInstance(typeof(Int32), lengths, lowerBounds);
But it generated a run-time error:
System.InvalidCastException: 'Unable to cast object of type 'System.Int32[*]' to type 'System.Int32[]'.'
Any suggestions? Thnak you.
For example =>
Array myArray = Array.CreateInstance(typeof(int), new[] { 7 }, new[] { 1 });
You're trying to cast the return to a type that is not valid.
Your code would work if you just change the type of the receiving
variable and eliminate the cast:
Excellent - Thanks much for your help. Stay safe!