如何Func键< T,TResult>工作? [英] How does Func<T,TResult> Work?

查看:225
本文介绍了如何Func键< T,TResult>工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个独特的扩展方法,我可以像下面的标准通过。

  persons.Distinct(P => p.Name); 



我从网上的代码,但我有一个很难理解的目的 Func键< T,TResult> 。此外,当我说 P => p.Name 我在发送字符串 名称还是我送完整的对象吗?这里是新的直观方法:

 公共静态类ExtensionMethods 
{
公共静态的IEnumerable< T>不同的< T>(
本的IEnumerable< T>列表中,Func键< T,对象>检查)
{
返回list.Distinct(新GenericComparer< T>(检查));
}
}

公共类GenericComparer< T> :&的IEqualityComparer LT; T>
{
私人Func键< T,对象> _checker;

公共GenericComparer(Func键< T,对象>检查)
{
_checker =检查;
}

公共布尔等于(T X,T Y)
{
返回_checker(x)的.Equals(_checker(Y));
}

公众诠释的GetHashCode(T OBJ)
{
返回_checker(OBJ).GetHashCode();
}
}

和这里的用法:

 静态无效的主要(字串[] args)
{
变种人=新的List<人>()
{
新的Person(){ID = 1,名称=玛丽},
新的Person(){ID = 2,名称=约翰},
新的Person() {ID = 3,名称=玛丽}
};

VAR uniquePersons = persons.Distinct(P => p.Name);

的foreach(VAR人uniquePersons)
{
Console.WriteLine(person.Name);
}
}


解决方案

当你这样做:

  persons.Distinct(p => p.Name); 



你基本上动态创建一个函数(的使用lambda表达式),看起来像这样:

 字符串theFunction(人p)
{
返回p.Name;
}

这是一个适合的签名功能的 FUNC<人,字符串> 委托。独特的方法可以把它用来确定一个元素是否是不同的委托(基本上是一个函数指针) - 在你的情况下,只有唯一的字符串(由上面的函数返回)将被视为不同的元素。这种委托是你的人枚举的每个元素上运行,这些函数的结果被使用。然后创建一个序列(的IEnumerable<&人GT; ),由这些元素


I am creating a Distinct extension method where I can pass in the criteria like the following.

persons.Distinct(p => p.Name); 

I got the code from the web but I am having a hard time understanding the purpose of Func<T, TResult>. Also, when I say p => p.Name am I sending the String Name or am I sending the complete Person object? Here is the new Distinct method:

public static class ExtensionMethods
{
    public static IEnumerable<T> Distinct<T>(
        this IEnumerable<T> list, Func<T,object> checker)
    {
        return list.Distinct(new GenericComparer<T>(checker)); 
    }
}

public class GenericComparer<T> : IEqualityComparer<T>
{
    private Func<T, object> _checker; 

    public GenericComparer(Func<T,object> checker)
    {
        _checker = checker; 
    }

    public bool Equals(T x, T y)
    {
        return _checker(x).Equals(_checker(y));
    }

    public int GetHashCode(T obj)
    {
        return _checker(obj).GetHashCode(); 
    }
}

And here is the usage:

static void Main(string[] args)
{
    var persons = new List<Person>()
    {
        new Person() { Id = 1, Name = "Mary"}, 
        new Person() {Id = 2, Name="John"}, 
        new Person() { Id = 3, Name = "Mary"}
    };

    var uniquePersons = persons.Distinct(p => p.Name); 

    foreach(var person in uniquePersons)
    {
        Console.WriteLine(person.Name);
    }
}

解决方案

When you do this:

persons.Distinct(p => p.Name);

You're basically creating a function on the fly (using lambda expressions), that looks like this:

string theFunction(Person p)
{
    return p.Name;
}

This is a function that fits the signature of a Func<Person,String> delegate. The Distinct method can take a delegate (basically a function pointer) which it uses to determine whether or not an element is distinct - in your case, only unique strings (returned by the function above) will be considered "distinct" elements. This delegate is run on each element of your "persons" enumerable, and the results of those functions are used. It then creates a sequence (IEnumerable<Person>) from those elements.

这篇关于如何Func键&LT; T,TResult&GT;工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆