今天公司弄了个VS2008 beta 2中文版,虽然很大一部分是为了JS的智能提示外,也应该好好的温习一下.NET的一些新特性,由于以前写过一些文章,但是都没有系统的学过,都只是尝一下新鲜感。不知道从那开始,所以今天就来看看自动话属性,以及对象初始化和集合初始化的一些新特性。
1.自动属性
在做一个程序是我们离不开属性,特别是实体类。我们用指头一个一个的敲着get和set及局部的变量(Fields),现在可好不用在重复敲那些东东了我们只要用到get和set,就和我们的接口声明差不多了,是不是很happy。
OK看个例子先,在.NET 2.0下我们声明一个实体类要有如下做法,这个应该没什么意见吧。
public
class
Person {
private string firstName;
private string lastName;
private int age;
public string FirstName {
get {
return this .firstName;
}
set {
this .firstName = value;
}
}
public string LastName {
get {
return this .lastName;
}
set {
this .lastName = value;
}
}
public int Age {
get {
return this .age;
}
set {
this .age = value;
}
}
private string firstName;
private string lastName;
private int age;
public string FirstName {
get {
return this .firstName;
}
set {
this .firstName = value;
}
}
public string LastName {
get {
return this .lastName;
}
set {
this .lastName = value;
}
}
public int Age {
get {
return this .age;
}
set {
this .age = value;
}
}
在.NET 3.x中我们可以省了很多东东,代码也变得简单很多,代码如下:
public
class
Person {
public string FirstName {
get ; set ;
}
public string LastName {
get ; set ;
}
public int Age {
get ; set ;
}
public string FirstName {
get ; set ;
}
public string LastName {
get ; set ;
}
public int Age {
get ; set ;
}
2.对象的初始化
我们省了很多的代码,手指也少动了,没办法人总是越来越懒的吗,下面我们来看看对象的初始化也是简单了不少,看代码就知道了,代码如下:第一个代码是.NET2.0中的初始化方式,而第二个是.NET 3.x的初始化方式,当然.NET2.0的方式也适合3.x的版本。
//
.NET 2.0:
Person person = new Person();
person.FirstName = " 小兵 " ;
person.LastName = " 网魂 " ;
person.Age = 23 ;
// .NET 3.x:
Person person = new Person { FirstName = " 小兵 " , LastName = " 网魂 " , Age = 23 }
Person person = new Person();
person.FirstName = " 小兵 " ;
person.LastName = " 网魂 " ;
person.Age = 23 ;
// .NET 3.x:
Person person = new Person { FirstName = " 小兵 " , LastName = " 网魂 " , Age = 23 }
3.集合的初始化
其实我们写程序也是封装在封装来为我们节省更多的东东,复用更多的东东。微软为我们做的也是越来越多,这是这篇文章的最后一个主题就是集合的初始化,我么一起来对照一下。
//
.NET 2.0;
List < Person > people = new List < Person > ();
people.Add( new Person { FirstName = " 小兵 " , LastName = " 网魂 " , Age = 23 });
people.Add( new Person { FirstName = " QQing " , LastName = " Lai " , Age = 22 });
people.Add( new Person { FirstName = " Xbing " , LastName = " My " , Age = 20 });
// .NET 3.x;
List < Person > people = new List < Person > {
new Person { FirstName = " 小兵 " , LastName = " 网魂 " , Age = 23 },
new Person { FirstName = " QQing " , LastName = " Lai " , Age = 22 },
new Person { FirstName = " Xbing " , LastName = " My " , Age = 20 }
}; // 记得这边是要有";"的,不然就会出错的
List < Person > people = new List < Person > ();
people.Add( new Person { FirstName = " 小兵 " , LastName = " 网魂 " , Age = 23 });
people.Add( new Person { FirstName = " QQing " , LastName = " Lai " , Age = 22 });
people.Add( new Person { FirstName = " Xbing " , LastName = " My " , Age = 20 });
// .NET 3.x;
List < Person > people = new List < Person > {
new Person { FirstName = " 小兵 " , LastName = " 网魂 " , Age = 23 },
new Person { FirstName = " QQing " , LastName = " Lai " , Age = 22 },
new Person { FirstName = " Xbing " , LastName = " My " , Age = 20 }
}; // 记得这边是要有";"的,不然就会出错的
OK以上就是我们今天所温习的一个简单的特性,利用它们我们可以很简单的来书写实体类了,虽然在程序中完全支持.NET 3.x在现阶段还是不可能的,作为程序员学习在学习才是正道。