Array.Clear() で 2 次元配列をクリアする
Array.Clear メソッドは、1 次元配列だけでなく、2 次元配列もクリアすることが出来ます。
using System; using System.Collections.Generic; using System.Linq; class Program { static void Display(int[,] ary) { for (int i = 0; i < ary.GetLength(0); i++) { for (int j = 0; j < ary.GetLength(1); j++) { Console.Write("{0} ", ary[i, j]); } Console.WriteLine(); } Console.WriteLine(); } static void Main() { var ary = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, }; Display(ary); // 1 2 3 // 4 5 6 Array.Clear(ary, 1, ary.Length - 1); // ary[0, 0] 以外をクリア Display(ary); // 1 0 0 // 0 0 0 Array.Clear(ary, 0, ary.Length); // 全ての要素をクリア Display(ary); // 0 0 0 // 0 0 0 } }
実行結果です。
1 2 3 4 5 6 1 0 0 0 0 0 0 0 0 0 0 0