C#3.0新特性之隱式類型化數(shù)組

字號(hào):

C#3.0這個(gè)特性是對(duì)隱式類型化本地變量的擴(kuò)展,有了這個(gè)特性,將使我們創(chuàng)建數(shù)組的工作變得簡單。我們可以直接使用"new[]"關(guān)鍵字來聲明數(shù)組,后面跟上數(shù)組的初始值列表。在這里,我們并沒有直接指定數(shù)組的類型,數(shù)組的類型是由初始化列表推斷出來的。
    以下是引用片段:
     class AnonymousTypeArray : AppRunner.AbstractApplication
     {
     public override void Run()
     {
     var intArray = new[] { 1, 2, 3, 4, 5 };
     var doubleArray = new[] { 3.14, 1.414 };
     var anonymousTypeArray = new[] {
     new { Name="van’s", Sex=false, Arg=22 },
     new { Name="martin", Sex=true, Arg=23 }
     };
     Console.WriteLine(intArray);
     Console.WriteLine(doubleArray);
     Console.WriteLine(anonymousTypeArray[0].Name);
     }
     }