惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Cloudflare Blog
U
Unit 42
F
Fortinet All Blogs
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
Y
Y Combinator Blog
罗磊的独立博客
V
Visual Studio Blog
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
爱范儿
爱范儿
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
I
InfoQ
博客园 - 叶小钗
博客园 - 聂微东
Last Week in AI
Last Week in AI

博客园 - On the road....

c# -- 对象销毁和垃圾回收 C#集合 -- Equality和Order插件 C#集合--Dictionary C#集合 -- 自定义集合与代理 C#集合 -- Lists,Queues, Stacks 和 Sets C#集合--ICollection接口和IList接口 C#集合--数组 C#集合-列举(Enumeration) C#排序比较 Customer IEnuramble Extension C#相等性比较 c#列举和迭代器 C#记录对象的变化 C#按需序列化对象为Json字符串 c#如何区分静态只读变量和常量 C#如何更好地理解引用类型和值类型 Pro ASP.NET MVC –第五章 使用Razor Pro ASP.NET MVC –第六章 MVC的基本工具 Pro ASP.NET MVC –第四章 语言特性精华
C#代理那点事儿
On the road.... · 2014-02-18 · via 博客园 - On the road....

Func代理是啥?

Func代理接收0个或多个参数,返回TResult值

以Func<TSource, TResult>为例:Func带来封装一个方法,该方法接收一个参数,然会一个TResult类型。

举个最简单的例子,求一个一维整数数组的和

private static void Demo()
{
    Func<int[], int> MySum = arr => 
    { 
        int total = 0;
        foreach (int i in arr)
            total += i;

        return total;
    };

    int[] data = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int result = MySum(data);

    Console.WriteLine(result);
}

Sum

OK,我们继续,如果我们希望复杂一点:先对数组过滤,然后求和,那么我们的代码应该如下:

private static void Demo3()
{

    int[] data = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int result = data.FilterAndSum(a => a > 7);

    Console.WriteLine(result);
}

public static class MyExtension
{
    public static int FilterAndSum(this int[] source, Func<int, bool> selector)
{ 
   int total = 0;
   foreach (int s in source)
       if (selector(s))
           total += s;

   return total;
}   
}

FilterAndSum

如果,我们希望我们的扩展方法,可以支持更改多的类型,那么我们可以这样的实现扩展方法

public static int SelectAndSum<TSource>(this IList<TSource> source, Func<TSource, int> selector)
{
    int total = 0;

    foreach (TSource s in source)
        total += selector(s);

    return total;
}

......


private static void Demo()
{
    IList<Staff> list = new List<Staff> { 
        new Staff{FirstName="AAA", LastName="111", Salary=1000},
        new Staff{FirstName="BBB", LastName="222", Salary=2000}
    };
    int result = list.SelectAndSum(s => s.Salary);

    Console.WriteLine(result);
}

Generic Extend method

另外,如果我们想自己扩展IEnumerable,那么现在我们就知道应该这样:

public static IEnumerable<TSource> CustomMethod<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> selector)
{
    foreach(TSource s in source)
        if(selector(s))
            yield return s;
}
...

private static void Demo4()
{
    IList<Staff> list = new List<Staff> { 
        new Staff{FirstName="Tom", LastName="Ng", Salary=1000},
        new Staff{FirstName="Simon", LastName="Wong", Salary=2000}
    };

    IEnumerable<Staff> list2 = list.CustomMethod(s => s.Salary > 1000);


    Console.WriteLine(list2.Count());
}

Extend IEnumerable

那么Action代理呢

Action代理不返回值,它可以接受一个或多个参数。

那么Delegete类呢?

代理就是一个指向某个类的静态方法,或者指向实例类的实例方法。

那么代理呢?

代理是定义了方法签名的类型。当你实例化一个代理类型时,你可以把该代理实例指向与代理签名相兼容的方法。然后通过代理实例调用所指向的方法

posted @ 2014-02-18 17:29  On the road....  阅读(846)  评论()    收藏  举报