配列を特定の値で初期化する拡張メソッド

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; // StringBuilder

static class ArrayExtensions {
    public static void Fill<T>(this T[] xs, T val) {
        for (int i = 0; i < xs.Length; i++) {
            xs[i] = val;
        }
    }

    public static void Fill<T>(this T[,] xs, T val) {
        for (int i = 0; i < xs.GetLength(0); i++) {
            for (int j = 0; j < xs.GetLength(1); j++) {
                xs[i, j] = val;
            }
        }
    }
}

class Program {
    static void Display(int[] xs) {
        Console.WriteLine("[{0}]", string.Join(",", xs));
    }

    static void Display(int[,] xs) {
        var ls = new List<string>();
        for (int i = 0; i < xs.GetLength(0); i++) {
            var sb = new StringBuilder();
            sb.Append('[');
            for (int j = 0; j < xs.GetLength(1); j++) {
                if (j > 0) sb.Append(',');
                sb.Append(xs[i, j]);
            }
            sb.Append(']');
            ls.Add(sb.ToString());
        }
        Console.WriteLine("[{0}]", string.Join(",", ls));
    }

    static void Main() {
        int[] xs = new int[3];
        Display(xs); // [0,0,0]

        xs.Fill(10); // 全ての要素を 10 にする
        Display(xs); // [10,10,10]

        int[,] ys = new int[2, 3];
        Display(ys); // [[0,0,0],[0,0,0]]

        ys.Fill(5); // 全ての要素を 5 にする
        Display(ys); // [[5,5,5],[5,5,5]]
    }
}