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
1. Create: Used to Create a new empty file. Syntax in C language: int create(char *filename, mode_t mode) Parameter: filename : name of the file which you want to create.
c file contains the main function, the server. c file contains the user defined function, The third file which is server. h header file which calls the user defined functions in the server. c file and the fourth file which is makefile.mk which contains set of all the commands with their variable names.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With