不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
通过ildasm查看省略get,set块代码的区别
编辑:dnawo 日期:2011-01-15
平时在定义类属性时一般都这么写:
前几天看别人的代码,才知道get,set块中的代码可以省略的,并且使用起来也没什么不同:
它们有什么区别呢?打开ildasm看了下:


很明显,省略get,set块中的代码后编译器自动声明两个字段<Name>k__BackingField和<Name>k__BackingField,等效于我们手工声明的name和age。再看下MSIL代码:
当然了,这么省略也是有前提条件的,那就是不需要对属性进行一些额外的处理,比如赋值时不需检测值的有效性,否则还是不能省略的。
复制内容到剪贴板
程序代码

class Person
{
private string name;
private int age;
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}
{
private string name;
private int age;
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}
前几天看别人的代码,才知道get,set块中的代码可以省略的,并且使用起来也没什么不同:
复制内容到剪贴板
程序代码

class Person
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
}
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
}
它们有什么区别呢?打开ildasm看了下:


很明显,省略get,set块中的代码后编译器自动声明两个字段<Name>k__BackingField和<Name>k__BackingField,等效于我们手工声明的name和age。再看下MSIL代码:
复制内容到剪贴板
程序代码

.method public hidebysig specialname instance string
get_Name() cil managed
{
// 代码大小 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld string ConsoleApplication1.Person::name
IL_0006: ret
} // end of method Person::get_Name
.method public hidebysig specialname instance void
set_Name(string 'value') cil managed
{
// 代码大小 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld string ConsoleApplication1.Person::name
IL_0007: ret
} // end of method Person::set_Name
get_Name() cil managed
{
// 代码大小 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld string ConsoleApplication1.Person::name
IL_0006: ret
} // end of method Person::get_Name
.method public hidebysig specialname instance void
set_Name(string 'value') cil managed
{
// 代码大小 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld string ConsoleApplication1.Person::name
IL_0007: ret
} // end of method Person::set_Name
复制内容到剪贴板
程序代码

.method public hidebysig specialname instance string
get_Name() cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
// 代码大小 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld string ConsoleApplication1.Person::'<Name>k__BackingField'
IL_0006: ret
} // end of method Person::get_Name
.method public hidebysig specialname instance void
set_Name(string 'value') cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
// 代码大小 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld string ConsoleApplication1.Person::'<Name>k__BackingField'
IL_0007: ret
} // end of method Person::set_Name
get_Name() cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
// 代码大小 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld string ConsoleApplication1.Person::'<Name>k__BackingField'
IL_0006: ret
} // end of method Person::get_Name
.method public hidebysig specialname instance void
set_Name(string 'value') cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
// 代码大小 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld string ConsoleApplication1.Person::'<Name>k__BackingField'
IL_0007: ret
} // end of method Person::set_Name
当然了,这么省略也是有前提条件的,那就是不需要对属性进行一些额外的处理,比如赋值时不需检测值的有效性,否则还是不能省略的。
评论: 0 | 引用: 0 | 查看次数: 4864
发表评论
请登录后再发表评论!