2、初始化器。
3、匿名类型。
4、扩展方法。
5 、Lambda表达式。
6、简单的Linq。
一、自动属性。
先看一个framework 2.0的里属性:
1 int m_nID;
2 string m_strSex;
3 string m_strName;
4 int m_nAge;
5
6 public int ID
7 {
8 get
9 {
10 return m_nID;
11 }
12
13 set
14 {
15 m_nID = value;
16 }
17 }
18
19 public string Sex
20 {
21 get
22 {
23 return m_strSex;
24 }
25
26 set
27 {
28 m_strSex = value;
29 }
30 }
31
32 public string Name
33 {
34 get
35 {
36 return m_strName;
37 }
38
39 set
40 {
41 m_strName = value;
42 }
43 }
2 string m_strSex;
3 string m_strName;
4 int m_nAge;
5
6 public int ID
7 {
8 get
9 {
10 return m_nID;
11 }
12
13 set
14 {
15 m_nID = value;
16 }
17 }
18
19 public string Sex
20 {
21 get
22 {
23 return m_strSex;
24 }
25
26 set
27 {
28 m_strSex = value;
29 }
30 }
31
32 public string Name
33 {
34 get
35 {
36 return m_strName;
37 }
38
39 set
40 {
41 m_strName = value;
42 }
43 }
在回头看看framework 3.5里的属性:
1 public int ID { get; set; }//也可给set加修饰符如:public int ID{get;private set;}
2 public string Sex { get; set; }
3 public string Name { get; set; }
4 public int Age { get; set; }
2 public string Sex { get; set; }
3 public string Name { get; set; }
4 public int Age { get; set; }
比较一下两段代码段,是不是简明多了。你是不是开始兴趣了。。。。。呵呵
2、初始化器。
初始化器从字面意思就能看出来它是初始化对象用的。。。。。
先把上面的属性封装到名为Student类中。现在你就可以这样的去给属性给值啦。注如果刚刚在写属性时间给它加上了public int ID{get; private set ;},在这里初始化肯定会报错的啦,应该原因SB都知道吧。以前我们在C#2.0中可能要写在构造函数中才能这样给值。
framework 2.0
public Student(int id,string name)
{
id=this.id;
name = this.name;
}
{
id=this.id;
name = this.name;
}
framework 3.5
Student stu = new Student { ID = 1, Name = "Jely", Sex = "未知", Age = 23 };
初始化集合
framework 2.0
List<Student> listStu = new List<Student>();
listStu.Add(new Student { ID = 1, Name = "Jely", Sex = "未知", Age = 23 });
listStu.Add(new Student { ID = 2, Name = "Jim", Sex = "未知", Age = 24 });
listStu.Add(new Student { ID = 3, Name = "Petter", Sex = "未知", Age = 25 });
foreach (Student item in listStu)
{
Console.Write("ID:" + item.ID + "\nName:" + item.Name+"\n");
}
listStu.Add(new Student { ID = 1, Name = "Jely", Sex = "未知", Age = 23 });
listStu.Add(new Student { ID = 2, Name = "Jim", Sex = "未知", Age = 24 });
listStu.Add(new Student { ID = 3, Name = "Petter", Sex = "未知", Age = 25 });
foreach (Student item in listStu)
{
Console.Write("ID:" + item.ID + "\nName:" + item.Name+"\n");
}
framework 3.5
List<Student> listStu = new List<Student>
{
new Student { ID = 1, Name = "Jely", Sex = "未知", Age = 23 },
new Student { ID = 2, Name = "Jim", Sex = "未知", Age = 24 },
new Student { ID = 3, Name = "Petter", Sex = "未知", Age = 25 },
};
foreach (var item in listStu) //var和javascript中一样的用法,称为匿名类型
{
Console.Write("ID:" + item.ID + "\nName:" + item.Name + "\n");
}
{
new Student { ID = 1, Name = "Jely", Sex = "未知", Age = 23 },
new Student { ID = 2, Name = "Jim", Sex = "未知", Age = 24 },
new Student { ID = 3, Name = "Petter", Sex = "未知", Age = 25 },
};
foreach (var item in listStu) //var和javascript中一样的用法,称为匿名类型
{
Console.Write("ID:" + item.ID + "\nName:" + item.Name + "\n");
}
3、匿名类型。
实其这个东西我们自一开始在javascript中就以经用过了,只是MS现在给它取了个名字罢了,它就是Var,
大家看到这个不是像看到亲娘啊,呵呵,开玩笑的啦!像VB中的Dim也跟它差不多吧
var i=1;
var name="Jely"
这样的声明变量,可以称作为匿名类型,在编辑时,它会根据变量值自动识别此变量的数据类型。var name="Jely"
像上面的
foreach (var item in listStu) //var和javascript中一样的用法,称为匿名类型
{
Console.Write("ID:" + item.ID + "\nName:" + item.Name + "\n");
}
{
Console.Write("ID:" + item.ID + "\nName:" + item.Name + "\n");
}
4、扩展方法。
扩展方法指给X一个对象增加一个方法。
即然是对象增加一个方法,那么定义一个方法先。。。
public static class ExternMothod
{
#region 扩展方法
/// <summary>
/// 判断是否是偶数
/// </summary>
/// <param name="num"></param>
/// <returns>是则返回true,不是则返回false</returns>
public static bool IsEven(this int num)
{
bool flag=false;
if(num % 2==0)
{
flag=true;
}
return flag;
}
#endregion
}
调用:{
#region 扩展方法
/// <summary>
/// 判断是否是偶数
/// </summary>
/// <param name="num"></param>
/// <returns>是则返回true,不是则返回false</returns>
public static bool IsEven(this int num)
{
bool flag=false;
if(num % 2==0)
{
flag=true;
}
return flag;
}
#endregion
}
var i=1;
Console.write(i.IsEven)
在没有定义IsEven这个方法之前,变量i是没有的。就像每个对象都有一个.ToString()方法是一样的原理。Console.write(i.IsEven)
5、Lambda表达式
Lambda表达式其实在C#2.0中是用委托来实现的,在C#3.5中就很简单了。。。
List<Student> listStu = new List<Student>
{
new Student { ID = 1, Name = "Jely", Sex = "未知", Age = 23 },
new Student { ID = 2, Name = "Jim", Sex = "未知", Age = 24 },
new Student { ID = 3, Name = "Petter", Sex = "未知", Age = 25 },
};
IEnumerable<Student> result = listStu.Where(s => s.Age == 23);
Double a = listStu.Average(s => s.Age);//求平均数
int sum=listStu.Sum(s => s.Age);//求和
//遍历输入所过滤的结果
foreach (var item in result)
{
Console.Write("\nName:"+item.Name+" Average: "+a.ToString()+" SumAge:"+sum);
}
{
new Student { ID = 1, Name = "Jely", Sex = "未知", Age = 23 },
new Student { ID = 2, Name = "Jim", Sex = "未知", Age = 24 },
new Student { ID = 3, Name = "Petter", Sex = "未知", Age = 25 },
};
IEnumerable<Student> result = listStu.Where(s => s.Age == 23);
Double a = listStu.Average(s => s.Age);//求平均数
int sum=listStu.Sum(s => s.Age);//求和
//遍历输入所过滤的结果
foreach (var item in result)
{
Console.Write("\nName:"+item.Name+" Average: "+a.ToString()+" SumAge:"+sum);
}
6、Linq
我个人理解Linq就是.NET中的ORM,有点像Hibneter。MS把Linq作为framework 3.x的重点对象。上面的所有一切其实都是为Linq做准备的。它可以在.NET中像SQL一样操作数据。但写法不同于SQL。
下面我们来看一段代码吧
List<Student> listStu = new List<Student>
{
new Student { ID = 1, Name = "Jely", Sex = "未知", Age = 23 },
new Student { ID = 2, Name = "Jim", Sex = "未知", Age = 24 },
new Student { ID = 3, Name = "Petter", Sex = "未知", Age = 25 },
};
//select * from Student where Age=25
IEnumerable<Student> result = from s in listStu where s.Age == 25 select s;
//select * from Student Order by Age DESC
IEnumerable<Student> result1 = from s1 in listStu orderby s1.Age descending select s1;
foreach (var item in result1)
{
Console.Write("\nName:"+item.Name);
}
//select * from Student where Age=25{
new Student { ID = 1, Name = "Jely", Sex = "未知", Age = 23 },
new Student { ID = 2, Name = "Jim", Sex = "未知", Age = 24 },
new Student { ID = 3, Name = "Petter", Sex = "未知", Age = 25 },
};
//select * from Student where Age=25
IEnumerable<Student> result = from s in listStu where s.Age == 25 select s;
//select * from Student Order by Age DESC
IEnumerable<Student> result1 = from s1 in listStu orderby s1.Age descending select s1;
foreach (var item in result1)
{
Console.Write("\nName:"+item.Name);
}
IEnumerable<Student> result = from s in listStu where s.Age == 25 select s;
//select * from Student Order by Age DESC
IEnumerable<Student> result1 = from s1 in listStu orderby s1.Age descending select s1;
上面这两句子就是Linq的用法了。是不是和SQL很像。只是把select 放到最后,我们习惯就好了。
Linq有跟SQL一样强大的查询功能。它也有join等一系列的操作。