LoginSignup
6
7

More than 5 years have passed since last update.

C#で多次元配列を1次元配列のように扱う

Last updated at Posted at 2014-10-15

コード


using System;
using System.Linq;
using System.Collections.Generic;

public class IEnumerableTest
{
    public static void Main()
    {
        int [,] ary = new int[3, 3] {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}                    
        };
        foreach(var x in ary.Cast<int>())
        {
            Console.WriteLine(x);
        }
    }       
}       

出力結果

1
2
3
4
5
6
7
8
9

ちなみに

var xs = new int[3, 3]{ ... };
とかした後に
xs[1]
とかすると
IEnumerable.cs(14,30): error CS0022: Wrong number of indexes 1' inside [], expected2'
と怒られます。

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7