Whenever you allocate a new array in C # using
new T[length]
array entries have a default value for T. This is null for the case when T is a reference type or the result of the default constructor T , if T is the value type.
In my case, I want to initialize an Int32 array with a value of -1:
var myArray = new int[100]; for (int i=0; i<myArray.Length; i++) { myArray[i] = -1; }
So, after the memory is reserved for the array, the CLR iterates over the new allocated memory and sets all the entries to default (int) = 0. After that, my code sets all the entries to -1.
This makes initialization redundant. Does JIT describe this and neglect initialization to 0, and if not, is there a way to directly initialize a piece of memory with a user-defined value?
Referring to initializing a C # array with a value other than the default , using Enumerable.Repeat(value, length).ToArray() not an option because Enumerable.ToArray allocates a new array and copies the values ββto it afterwards.
arrays initialization c # constants
Rauhotz Mar 15 '09 at 23:37 2009-03-15 23:37
source share