Func如何显示 工作?
我正在创建一个不同的扩展方法,我可以在其中传递如下标准。
persons.Distinct(p => p.Name);
我从网上获取了代码,但我很难理解 Func
的用途。 另外,当我说 p =>; p.Name
我是发送 String
Name
还是发送完整的 Person
对象? 这是新的 Distinct 方法:
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();
}
}
这是用法:
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您这样做时:
您基本上是在动态创建一个函数(使用 lambda 表达式),看起来像这样:
这是一个符合
Func
委托签名的函数。 Distinct 方法可以采用一个委托(基本上是一个函数指针),它用它来确定元素是否不同 - 在您的情况下,只有唯一的字符串(由上面的函数返回)才会被视为“不同”元素。 该委托在“persons”可枚举的每个元素上运行,并使用这些函数的结果。 然后,它根据这些元素创建一个序列 (IEnumerable
)。When you do this:
You're basically creating a function on the fly (using lambda expressions), that looks like this:
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.您将返回不同的人,假设两个人具有相同的名称,则他们是相同的
如果您想要一组不同的名称,可以使用以下命令:
You are getting back the distinct People, under the assumption that two People are the same if they have the same name
If you want a distinct set of names, you can use this:
定义一个函数,该函数接受一个参数(类型为 T)并返回一个对象(类型为 TResult)。
在你的情况下,如果你想要一个接受 Person 对象并返回一个字符串的函数......你会想要
它相当于:
defines a function that accepts one parameter (of type T) and returns an object (of type TResult).
In your case, if you want a function that takes a Person object and returns a string...you'd want
which is the equivalent of: