您的位置:首页 > 编程语言 > C#

C#2.0 中的匿名方法

2012-12-09 15:36 477 查看
C# 2.0中采用了匿名方法,它允许我们能以一种直观的方式理解委托。匿名方法允许我们直接对一个委托对象定义代码段。当我们创建一个仅有小段代码的委托的时候,匿名方法提供了更为灵活的解决之路。让我们来看看下面这段代码:

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#编译器对这两个匿名方法创建了两个私有静态方法,然后用这些静态方法的地址来替代了匿名方法。如下图 所示,而编译器创建静态方法或者实例方法,这取决于匿名方法内部的代码段。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: