You can create an extension method to initialize the array, for example:
public static void InitAll<T>(this T[] array, T value) { for (int i = 0; i < array.Length; i++) { array[i] = value; } }
and use it as follows:
bool[] buffer = new bool[128]; buffer.InitAll(true);
Edit:
To solve any problems that are not suitable for reference types, the simple task is to extend this concept. For example, you can add overload
public static void InitAll<T>(this T[] array, Func<int, T> initializer) { for (int i = 0; i < array.Length; i++) { array[i] = initializer.Invoke(i); } } Foo[] foos = new Foo[5]; foos.InitAll(_ => new Foo());
This will create 5 new Foo instances and assign them to the foos array.
source share