The fastest method I have found uses Array.Copy with the copy size doubling each time through the loop. The speed is basically the same whether you fill the array with a single value or an array of values.
In my test with 20,000,000 array items, this function is twice as fast as a for loop.
using System; namespace Extensions { public static class ArrayExtensions { public static void Fill<T>(this T[] destinationArray, params T[] values) { if (destinationArray == null) throw new ArgumentNullException(nameof(destinationArray)); Array.Copy(values, destinationArray, Math.Min(values.Length, destinationArray.Length)); if (values.Length >= destinationArray.Length) return; int arrayToFillHalfLength = destinationArray.Length / 2; int copyLength; for (copyLength = values.Length; copyLength < arrayToFillHalfLength; copyLength <<= 1) { Array.Copy(destinationArray, 0, destinationArray, copyLength, copyLength); } Array.Copy(destinationArray, 0, destinationArray, copyLength, destinationArray.Length - copyLength); } } }
I blogged about this at https://grax32.com/2011/11/initialize-array-to-value-in-c-very.html and https://grax32.com/2014/04/better-array-fill-function.html