C# 2.0
语言和编译器中的新增功能
1.匿名方法
可以将代码块作为参数来传递。在本应使用委托的任何地方,都可以使用代码块来取代:不需要定义新的方法。
下面的示例演示实例化委托的两种方法:
·
使委托与匿名方法关联。
·
使委托与命名方法 (DoWork) 关联。
// Declare a delegate
delegate void Printer(string s);
class TestClass
{
static void Main()
{
// Instatiate the delegate type using an anonymous method:
Printer p = delegate(string j)
{
System.Console.WriteLine(j);
};
// Results from the anonymous delegate call:
p("The delegate using the anonymous method is called.");
// The delegate instantiation using a named method "DoWork":
p = new Printer(TestClass.DoWork);
// Results from the old style delegate call:
p("The delegate using the named method is called.");
}
// The method associated with the named delegate:
static void DoWork(string k)
{
System.Console.WriteLine(k);
}
}
2.静态类
若要声明那些包含不能实例化的静态方法的类,静态类就是一种安全而便利的方式。C# 1.2 版要求将类构造函数定义为私有的,以防止类被实例化。
static class CompanyInfo
{
public static string GetCompanyName() { return "CompanyName"; }
public static string GetCompanyAddress() { return "mpanyAddress"; }
}
3.泛型
泛型概述:
·
使用泛型类型可以最大限度地重用代码、保护类型的安全以及提高性能。
·
泛型最常见的用途是创建集合类。
·
.NET Framework
类库在 System.Collections.Generic 命名空间中包含几个新的泛型集合类。应尽可能地使用这些类来代替普通的类,如 System.Collections 命名空间中的 ArrayList。
·
您可以创建自己的泛型接口、泛型类、泛型方法、泛型事件和泛型委托。
·
可以对泛型类进行约束以访问特定数据类型的方法。
·
关于泛型数据类型中使用的类型的信息可在运行时通过反射获取。
// Declare the generic class
public class GenericList<T>
{
void Add(T input) { }
}
class TestGenericList
{
private class ExampleClass { }
static void Main()
{
// Declare a list of type int
GenericList<int> list1 = new GenericList<int>();
// Declare a list of type string
GenericList<string> list2 = new GenericList<string>();
// Declare a list of type ExampleClass
GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
}
}
4.迭代器
迭代器概述
·
迭代器是可以返回相同类型的值的有序序列的一段代码。
·
迭代器可用作方法、运算符或
get
访问器的代码体。
·
可以在类中实现多个迭代器。每个迭代器都必须像任何类成员一样有唯一的名称,并且可以在
foreach
语句中被客户端代码调用,如下所示:
foreach(int x in SampleClass.Iterator2){}
迭代器对集合类特别有用,它提供一种简单的方法来迭代不常用的数据结构(如二进制树)。
示例说明
在本示例中,DaysOfTheWeek 类是将一周中的各天作为字符串进行存储的简单集合类。foreach 循环每迭代一次,都返回集合中的下一个字符串。
public class DaysOfTheWeek : System.Collections.IEnumerable
{
string[] m_Days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };
public System.Collections.IEnumerator GetEnumerator()
{
for (int i = 0; i < m_Days.Length; i++)
{
yield return m_Days[i];
}
}
}
class TestDaysOfTheWeek
{
static void Main()
{
// Create an instance of the collection class
DaysOfTheWeek week = new DaysOfTheWeek();
// Iterate with foreach
foreach (string day in week)
{
System.Console.Write(day + " ");
}
}
}
5. 分部类
·
处理大型项目时,使一个类分布于多个独立文件中可以让多位程序员同时对该类进行处理。
·
使用自动生成的源时,无需重新创建源文件便可将代码添加到类中。Visual Studio 在创建 Windows 窗体、Web 服务包装代码等时都使用此方法。您无需编辑 Visual Studio 所创建的文件,便可创建使用这些类的代码。
·
若要拆分类定义,请使用 partial 关键字修饰符,如下所示:
public partial class Employee
{
public void DoWork()
{
}
}
public partial class Employee
{
public void GoToLunch()
{
}
}
6.可空类型
可空类型是
System.Nullable
结构的实例。可空类型可以表示其基础值类型正常范围内的值,再加上一个
null
值。例如,
Nullable<Int32>
,读作“可空的
Int32
”,可以被赋值为
-2147483648
到
2147483647
之间的任意值,也可以被赋值为
null
值。
Nullable<bool>
可以被赋值为
true
或
false
,或
null
。在处理数据库和其他包含可能未赋值的元素的数据类型时,将
null
赋值给数值类型或布尔型的功能特别有用。例如,数据库中的布尔型字段可以存储值
true
或
false
,或者,该字段也可以未定义。