匿名类型(Anonymouse Type)
2007-11-21 09:52
134 查看
概述:
在初始化的时候根据初始化列表自动产生类型的一种机制。
实例代码:
public static void Main()
.method public hidebysig static void Main() cil managed
{
.entrypoint
// Code size 51 (0x33)
.maxstack 4
.locals init ([0] class '<>f__AnonymousType0`3'<int32,int32,string> x)
IL_0000: nop
IL_0001: ldc.i4.2
IL_0002: ldc.i4.4
IL_0003: ldstr "this is a string"
IL_0008: newobj instance void class '<>f__AnonymousType0`3'<int32,int32,string>::.ctor(!0,
!1,
!2)
IL_000d: stloc.0
IL_000e: ldloc.0
IL_000f: callvirt instance !0 class '<>f__AnonymousType0`3'<int32,int32,string>::get_a()
IL_0014: call void [mscorlib]System.Console::WriteLine(int32)
IL_0019: nop
IL_001a: ldloc.0
IL_001b: callvirt instance !1 class '<>f__AnonymousType0`3'<int32,int32,string>::get_b()
IL_0020: call void [mscorlib]System.Console::WriteLine(int32)
IL_0025: nop
IL_0026: ldloc.0
IL_0027: callvirt instance !2 class '<>f__AnonymousType0`3'<int32,int32,string>::get_c()
IL_002c: call void [mscorlib]System.Console::WriteLine(string)
IL_0031: nop
IL_0032: ret
} // end of method NewTest::Main
由此可见,在编译的时候确实已经已经根据列表中的值确定了其类型。
运行结果:
2
4
this is a string
在初始化的时候根据初始化列表自动产生类型的一种机制。
实例代码:
public static void Main()
.method public hidebysig static void Main() cil managed
{
.entrypoint
// Code size 51 (0x33)
.maxstack 4
.locals init ([0] class '<>f__AnonymousType0`3'<int32,int32,string> x)
IL_0000: nop
IL_0001: ldc.i4.2
IL_0002: ldc.i4.4
IL_0003: ldstr "this is a string"
IL_0008: newobj instance void class '<>f__AnonymousType0`3'<int32,int32,string>::.ctor(!0,
!1,
!2)
IL_000d: stloc.0
IL_000e: ldloc.0
IL_000f: callvirt instance !0 class '<>f__AnonymousType0`3'<int32,int32,string>::get_a()
IL_0014: call void [mscorlib]System.Console::WriteLine(int32)
IL_0019: nop
IL_001a: ldloc.0
IL_001b: callvirt instance !1 class '<>f__AnonymousType0`3'<int32,int32,string>::get_b()
IL_0020: call void [mscorlib]System.Console::WriteLine(int32)
IL_0025: nop
IL_0026: ldloc.0
IL_0027: callvirt instance !2 class '<>f__AnonymousType0`3'<int32,int32,string>::get_c()
IL_002c: call void [mscorlib]System.Console::WriteLine(string)
IL_0031: nop
IL_0032: ret
} // end of method NewTest::Main
由此可见,在编译的时候确实已经已经根据列表中的值确定了其类型。
运行结果:
2
4
this is a string
相关文章推荐
- C#3.0技术探讨(1):匿名类型 Anonymouse Type
- 匿名类型(C# 编程指南)
- 15.C#回顾及匿名类型(八章8.1-8.5)
- C#教程:关于匿名类型的特性
- 对匿名类型的幻想
- C# 3.0新特性初步研究 Part5:匿名类型_C#教程
- 匿名类型与隐式类型局部变量
- 为什么匿名内部类参数必须为final类型
- 单表多条件查询,匿名类型,匿名对象,嵌套查询,Linq序列转换后调用外部方法
- [C#基础知识]专题十三:全面解析对象集合初始化器、匿名类型和隐式类型
- C#2008语言特征6----匿名类型
- 如何把匿名类型.GetType()返回的对象传进泛型里面[转]
- 深入理解 c# 第五章 匿名方法用于Action<T>委托类型 反转字符 求平方根 求平均数
- 使用ExposedObject对Asp.net MVC中匿名类型的JsonResult做单元测试
- 为什么匿名内部类参数必须为final类型
- C#编程(十六)----------匿名类型
- C#的匿名类型
- C#3.0新特性――隐含类型局部变量和匿名类型
- 为什么匿名内部类参数必须为final类型
- 为什么匿名内部类参数必须为final类型