C#2.0 中的匿名方法

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




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值