驚愕の事実

以下、先日言及したコレクションの変換に関して、いろいろと試している中で初めて知ったこと。

static void Main(string[] args)
{
    object foo = new string[0];

    System.Console.WriteLine("foo type : " + foo.GetType());
    System.Console.WriteLine("foo is Array : " + (foo is Array));
    System.Console.WriteLine("foo is IList : " + (foo is IList));
    System.Console.WriteLine("foo is IList : " + (foo is IList));
    System.Console.WriteLine("foo is List : " + (foo is List));
}

この実行結果は、以下のようになる。

foo type : System.String[]
foo is Array : True
foo is IList : True
foo is IList : True
foo is List : False

(foo is IList)が真なのは当たり前なのだが、まさか(foo is IList)が真だとは......

正直なところ、一から勉強しなおそうと思った。

ちなみに、ジェネリックの型パラメタをに変えても、結果は変わらない。

foo type : System.String[]
foo is Array : True
foo is IList : True
foo is IList : True
foo is List : False

うおー。完全に自信喪失。