C#2.0 中的匿名方法
2012-12-09 15:36
477 查看
C# 2.0中采用了匿名方法,它允许我们能以一种直观的方式理解委托。匿名方法允许我们直接对一个委托对象定义代码段。当我们创建一个仅有小段代码的委托的时候,匿名方法提供了更为灵活的解决之路。让我们来看看下面这段代码:
我们再写出如下的代码来引用上面的类:
如果采用匿名方法,代码将变得更加自然。下面是采用匿名方法修改过后的代码:
匿名方法总是从一个delegate关键字,后面跟着参数内使用的方法和方法体本身。从上面的代码示例中,用户不需要指定的匿名方法的返回类型,它是根据在方法体内部的返回状态来推断出来的。
.NET CLR并不能执行自由的匿名代码段,
CLR要求它执行的是每一个方法的类型应该是一个静态方法或实例方法。所以,当你在类中写匿名方法并且编译代码,C#编译器在后台针对这些匿名方法来创建静态或实例方法。因此,匿名方法只是一个方便的语法来定义自己的类内部的方法传递给委托(代表处理程序/事件处理程序)。
当你编译了上面这个例子后,C#编译器对这两个匿名方法创建了两个私有静态方法,然后用这些静态方法的地址来替代了匿名方法。如下图 所示,而编译器创建静态方法或者实例方法,这取决于匿名方法内部的代码段。
public class MyCollection { public delegate bool SelectItem(string sItem); public string[] GetFilteredItemArray(SelectItem itemFilter) { List<string> sList = new List<string>(); foreach(string sItem in m_sList) { if (itemFilter(sItem) == true) sList.Add(sItem); } return sList.ToArray(); } public List<string> ItemList { get { return m_sList; } } private List<string> m_sList = new List<string>(); }
我们再写出如下的代码来引用上面的类:
public class Program { public static void Main(string[] args) { MyCollection objMyCol = new MyCollection(); objMyCol.ItemList.Add("Aditya"); objMyCol.ItemList.Add("Tanu"); objMyCol.ItemList.Add("Manoj"); objMyCol.ItemList.Add("Ahan"); objMyCol.ItemList.Add("Hasi"); // get an array of string items in the collection that start // with letter 'A' // string[] AStrings = objMyCol.GetFilteredItemArray(FilterStringWithA); Console.WriteLine("----- Strings starting with letter 'A' -----"); foreach(string s in AStrings) { Console.WriteLine(s); } // get an array of string items in the collection that start // with letter 'T' // string[] TStrings = objMyCol.GetFilteredItemArray(FilterStringWithT); Console.WriteLine("----- Strings starting with letter 'T' -----"); foreach(string s in TStrings) { Console.WriteLine(s); } } public static bool FilterStringWithA(string sItem) { if (sItem[0] == 'A') return true; else return false; } public static bool FilterStringWithT(string sItem) { if (sItem[0] == 'T') return true; else return false; } }
如果采用匿名方法,代码将变得更加自然。下面是采用匿名方法修改过后的代码:
public class Program { public delegate void MyDelegate(); public static void Main(string[] args) { MyCollection objMyCol = new MyCollection(); objMyCol.ItemList.Add("Aditya"); objMyCol.ItemList.Add("Tanu"); objMyCol.ItemList.Add("Manoj"); objMyCol.ItemList.Add("Ahan"); objMyCol.ItemList.Add("Hasi"); // get an array of string items in the collection that start // with letter 'A' // string[] AStrings = objMyCol.GetFilteredItemArray(delegate(string sItem) { if (sItem[0] == 'A') return true; else return false; }); Console.WriteLine("----- Strings starting with letter 'A' -----"); foreach (string s in AStrings) { Console.WriteLine(s); } // get an array of string items in the collection that start // with letter 'T' // string[] TStrings = objMyCol.GetFilteredItemArray(delegate(string sItem) { if (sItem[0] == 'T') return true; else return false; }); Console.WriteLine("----- Strings starting with letter 'T' -----"); foreach (string s in TStrings) { Console.WriteLine(s); } } }
匿名方法总是从一个delegate关键字,后面跟着参数内使用的方法和方法体本身。从上面的代码示例中,用户不需要指定的匿名方法的返回类型,它是根据在方法体内部的返回状态来推断出来的。
.NET CLR并不能执行自由的匿名代码段,
CLR要求它执行的是每一个方法的类型应该是一个静态方法或实例方法。所以,当你在类中写匿名方法并且编译代码,C#编译器在后台针对这些匿名方法来创建静态或实例方法。因此,匿名方法只是一个方便的语法来定义自己的类内部的方法传递给委托(代表处理程序/事件处理程序)。
当你编译了上面这个例子后,C#编译器对这两个匿名方法创建了两个私有静态方法,然后用这些静态方法的地址来替代了匿名方法。如下图 所示,而编译器创建静态方法或者实例方法,这取决于匿名方法内部的代码段。
相关文章推荐
- C# 2.0 Specification(匿名方法)(一)
- c#2.0的新功能(三):分部类、匿名方法等
- C# 2.0中的匿名方法
- C# 2.0 匿名方法与 Windows Forms 异步调用
- 对C# 2.0中匿名方法的怀疑分析
- C# 2.0 Specification(匿名方法)(一)
- C# 2.0 引入的匿名方法
- C# 2.0 Specification(匿名方法)(二)
- C#2.0 锐利体验系列课程(2):匿名方法、迭代器 (Level 300)
- C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- [读书笔记]C#学习笔记四: C#2.0泛型 可控类型 匿名方法和迭代器
- C#2.0新的语法扩充(泛型,迭代器,匿名方法)
- C# 2.0 匿名方法、迭代器
- C#2.0技术探讨(1):匿名方法
- c#2.0新特性:(二)匿名方法(Anonymous Methods)
- [读书笔记]C#学习笔记四: C#2.0泛型 可控类型 匿名方法和迭代器
- C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- C# 2.0 匿名方法简单解说
- 对C# 2.0中匿名方法的怀疑分析
- C# 2.0 Specification(匿名方法)(二)