Eric Yin
Eric Yin

Reputation: 8993

Create c# int[] with value as 0,1,2,3... length

I like to create an int[] with length X and value it with [0,1,2....X]

e.g.

public int[] CreateAA(int X){}

int[] AA = CreateAA(9) => [0,1,2,3,4,5,6,7,8,9]

is there any easy method? Or have to loop and init value

Upvotes: 53

Views: 86242

Answers (6)

Asif Mushtaq
Asif Mushtaq

Reputation: 13150

To initialize try this

int x = 10;
Enumerable.Range(0, x)
          .Select((v, i) => v + i).ToArray();

Upvotes: 2

Thanatos
Thanatos

Reputation: 1186

with function and loop:

static int[] f(int X)
{
    int[] a = new int[X+1];
    for(int i = 0; i < a.Length; i++)
        a[i] = i;
    return a;
}

Upvotes: 2

Martin Liversage
Martin Liversage

Reputation: 106906

Using Enumerable.Range(0, 10).ToArray() is very concise but if you want to create a very large array the ToArray extension method will have to collect the numbers into a buffer that will have to be reallocated multiple times. On each reallocation the contents of the buffer is copied to the new larger buffer. .NET uses a strategy where the size of the buffer is doubled on each reallocation (and the initial buffer has four items).

So if you want to avoid multiple reallocations of the buffer you need to create the array in advance:

int[] aa = new int[10];
for (var i = 0; i < aa.Length; i += 1)
  aa[i] = i;

This is the most efficient way of initializing the array.

However, if you need an array of say 100,000,000 consecutive numbers then you should look at a design where you don't have to keep all the numbers in an array to avoid the impact of the memory requirement. IEnumerable<int> is very useful for this purpose because you don't have to allocate the entire sequence but can produce it while you iterate and that is exactly what Enumerable.Range does. So avoiding the array of consecutive numbers in the first place may be even better than thinking about how to create it.

Upvotes: 12

Guffa
Guffa

Reputation: 700592

For completeness, here is a function that creates an array.

I made it a bit more versatile by having parameters for the min and max value, i.e. CreateArray(0, 9) returns {0,1,2,3,4,5,6,7,8,9}.

static int[] CreateArray(int min, int max) {
  int[] a = new int[max - min + 1];
  for (int i = 0; i < a.Length; i++) {
    a[i] = min + i;
  }
  return a;
}

Upvotes: 2

V4Vendetta
V4Vendetta

Reputation: 38230

You can avail the functionality of IEnumerable.

int[] arr = Enumerable.Range(0, X+1).ToArray();

This will create a IEnumerable List for you and .ToArray() will satisfy your int array need.

So for X=9 in your case it would generate the array for [0,1,2,3,4,5,6,7,8,9] (as you need)

Upvotes: 113

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48560

Why make a function when it is already there.

For this specific example, use

int[] AA = Enumerable.Range(0, 10).ToArray();

where 0 is the starting value and 10 (X + 1) is the length of array

So a general one applicable to all

int[] AA = Enumerable.Range(0, X + 1).ToArray();

Upvotes: 8

Related Questions