I want to write an extension method to populate a multidimensional rectangular array. I know how to do this for an array with a fixed number of dimensions:
public static void Fill<T>(this T[] source, T value) { for (int i = 0; i < source.Length; i++) source[i] = value; } public static void Fill<T>(this T[,] source, T value) { for (int i = 0; i < source.GetLength(0); i++) for (int j = 0; j < source.GetLength(1); j++) source[i, j] = value; } public static void Fill<T>(this T[,,] source, T value) { for (int i = 0; i < source.GetLength(0); i++) for (int j = 0; j < source.GetLength(1); j++) for (int k = 0; k < source.GetLength(2); k++) source[i, j, k] = value; }
Is it possible to write one fill method for an entire multidimensional rectangular array?
AndreyAkinshin
source share