How to create a single diminsional array starting from index 1 instead of zero

Emmad Kareem 81 Reputation points
2020-11-25T15:59:59.053+00:00

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.

C#
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.
10,526 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 82,501 Reputation points
    2020-11-25T16:34:53.177+00:00

    For example =>

    Array myArray = Array.CreateInstance(typeof(int), new[] { 7 }, new[] { 1 });
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful