Array.Clear() fills the arrays with the default value (zero for integers), I would like to fill it using -1 for example.
Thanks.
Array.Clear() fills the arrays with the default value (zero for integers), I would like to fill it using -1 for example.
Thanks.
The other way is:
int[] arr = Enumerable.Repeat(-1, 10).ToArray(); Console.WriteLine(string.Join(",",arr));
I am not aware of such a method. You could write one yourself, though:
public static void Init<T>(this T[] array, T value) { for(int i=0; i < array.Length; ++i) { array[i] = value; } }
You can call it like this:
int[] myArray = new int[5]; myArray.Init(-1);