Welcome

首页 / 软件开发 / C# / Linq学习(2) .NET 3.X新特性回顾

Linq学习(2) .NET 3.X新特性回顾2010-07-12 博客园 飘遥(周振兴)前面介绍了《C# 3.0 新特性》,对其注意事项没有过多的介绍,在这补充一下,回顾一下.NET 3.X的新特性。

自动属性(Automatic Properties)

不妨称自动属性之前的属性为传统属性。自动属性简化了语法,但也失掉了属性设置获取时进行操作的功能,也无法设置初始值。

若想只读或只写可在set或get前加上访问修饰符,设置的访问修饰符必须比属性本身的可访问性低,并且不能同时设置get和set的访问修饰符;internal和protected存在交集,因此不能同时设置属性和get或set分别为internal和protected。

下面的代码演示自动属性和传统属性:

public class Test1
{
public int Int0 { get; set; }
public int Int1 { private get; set; }
private int int2;
private int int3;
public int Int2
{
get { return int2; }
set { int2 = value; }
}
public int Int3
{
get { return int3; }
}
}

编译后用ILDasm查看如图: