「C#」for each文で2次元配列の要素を取得する
書式
データ型(int,string)[,] 配列名 = new データ型(int,string) [行数,列数]{
{xxx}
}
foreach (var 変数名 in 配列名) {
処理コード
}
データ型(int,string)[,] 配列名 = new データ型(int,string) [行数,列数]{
{xxx}
}
foreach (var 変数名 in 配列名) {
処理コード
}
データ型(int,string)[,] 配列名 = new データ型(int,string) [行数,列数]{ {xxx} } foreach (var 変数名 in 配列名) { 処理コード }
使用例
using System;
class Arkgame
{
public static void Main()
{
//2次元配列 2行 3列
int[,] cft = new int[2, 3]
{
{101, 102, 103},
{202, 203, 204}
};
Console.WriteLine("for each文で2次元配列の要素を取得");
foreach (var ele in cft)
{
Console.WriteLine(ele);
}
Console.WriteLine("2次元配列の数: " + cft.Length.ToString());
Console.ReadKey();
}
}
using System;
class Arkgame
{
public static void Main()
{
//2次元配列 2行 3列
int[,] cft = new int[2, 3]
{
{101, 102, 103},
{202, 203, 204}
};
Console.WriteLine("for each文で2次元配列の要素を取得");
foreach (var ele in cft)
{
Console.WriteLine(ele);
}
Console.WriteLine("2次元配列の数: " + cft.Length.ToString());
Console.ReadKey();
}
}
using System; class Arkgame { public static void Main() { //2次元配列 2行 3列 int[,] cft = new int[2, 3] { {101, 102, 103}, {202, 203, 204} }; Console.WriteLine("for each文で2次元配列の要素を取得"); foreach (var ele in cft) { Console.WriteLine(ele); } Console.WriteLine("2次元配列の数: " + cft.Length.ToString()); Console.ReadKey(); } }
結果
for each文で2次元配列の要素を取得
101
102
103
202
203
204
2次元配列の数: 6
for each文で2次元配列の要素を取得
101
102
103
202
203
204
2次元配列の数: 6
for each文で2次元配列の要素を取得 101 102 103 202 203 204 2次元配列の数: 6