小编Gae*_*anu的帖子

扩展方法,T的Func和T的列表

我想编写一个函数,可以在将值插入List之前完成一些检查.例如:

class Person {
    public string Name { get; set; }
    public int Value { get; set; }
    public Guid Id { get; set; }
}
-------
var persons = new List<Person>();
// add a new person if John doesn't exist
persons.AddIf(s => !s.Name.Equals("John"), new Person { ... });
----
public static void AddIf(this List<T> lst, Func<T, bool> check, T data)
{
     // how can I use the Func 'check' to check if exist an object with the
     // information …
Run Code Online (Sandbox Code Playgroud)

c# generics lambda extension-methods

6
推荐指数
1
解决办法
1355
查看次数

标签 统计

c# ×1

extension-methods ×1

generics ×1

lambda ×1