| 以下是引用片段: 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); } } |
上面的代码中,anonymousTypeArray变量的声明同时运用了隐式类型化数组和匿名类型两种特性,首先创建匿名类型,然后再初始值列表,推断出数组的确切类型。