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

推荐订阅源

The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Y
Y Combinator Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
博客园_首页
小众软件
小众软件
I
InfoQ
J
Java Code Geeks
月光博客
月光博客
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Latest news
Latest news
G
GRAHAM CLULEY
IT之家
IT之家
C
Cisco Blogs
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta
L
LangChain Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
T
Tenable Blog
博客园 - Franky
美团技术团队
I
Intezer
U
Unit 42
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
SegmentFault 最新的问题
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 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)  评论()    收藏  举报