In C# when you create an array, it is initialized with default value for each element.
var array = new int[1000];
// array elements are initialized with 0 by default.
Although it is a very good behavior which prevents lots of bugs, but in some situations initializing elements by default value may be unnecessary and lead to some performance pitfalls. For example:
while (someCondition)
{
var array = new int[10000];
for (var i = 0; i < 10000; i++)
array[i] = someMethod(i);
}
In this scenario the initialization of array
is totally unnecessary and in some cases may cause performance issues.
Question: Is there anyway to create an array not initializing its elements by some value (so each element has some random number in it)?