C# 你如何声明一个 Predicate Delegate 内联?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/59166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 10:17:10  来源:igfitidea点击:

How do you declare a Predicate Delegate inline?

提问by Curtis

I'm using C#.

我正在使用 C#。

So I have an object which has some fields, doesn't really matter what. I have a generic list of these objects.

所以我有一个包含一些字段的对象,这并不重要。我有这些对象的通用列表。

List<MyObject> myObjects = new List<MyObject>();
myObjects.Add(myObject1);
myObjects.Add(myObject2);
myObjects.Add(myObject3);

So I want to remove objects from my list based on some criteria. For instance, myObject.X >= 10.I would like to use the RemoveAll(Predicate<T> match)method for to do this.

所以我想根据一些标准从我的列表中删除对象。例如,myObject.X >= 10.我想使用RemoveAll(Predicate<T> match)方法来做到这一点。

I know I can define a delegate which can be passed into RemoveAll, but I would like to know how to define this inline with an anonymous delegate, instead of creating a bunch of delegate functions which are only used in once place.

我知道我可以定义一个可以传递给 RemoveAll 的委托,但我想知道如何使用匿名委托来定义这个内联,而不是创建一堆只在一次使用的委托函数。

采纳答案by Erik van Brakel

There's two options, an explicit delegate or a delegate disguised as a lamba construct:

有两个选项,一个显式委托或一个伪装成 Lamba 结构的委托:

explicit delegate

显式委托

myObjects.RemoveAll(delegate (MyObject m) { return m.X >= 10; });

lambda

拉姆达

myObjects.RemoveAll(m => m.X >= 10);


Addition:

添加:

Performance wise both are equal. As a matter of fact, both language constructs generate the same IL when compiled. This is because C# 3.0 is basically an extension on C# 2.0, so it compiles to C# 2.0 constructs :)

性能方面两者是平等的。事实上,两种语言结构在编译时都会生成相同的 IL。这是因为 C# 3.0 基本上是 C# 2.0 的扩展,所以它编译为 C# 2.0 构造:)

回答by Mark Brackett

The lambda C# 3.0 way:

lambda C# 3.0 方式:

myObjects.RemoveAll(m => m.x >= 10);

The anonymous delegate C# 2.0 way:

匿名委托 C# 2.0 方式:

myObjects.RemoveAll(delegate (MyObject m) {
   return m.x >= 10;
});

And, for the VB guys, the VB 9.0 lambda way:

而且,对于 VB 人员,VB 9.0 lambda 方式:

myObjects.RemoveAll(Function(m) m.x >= 10)

Unfortunately, VB doesn't support an anonymous delegate.

不幸的是,VB 不支持匿名委托。

回答by Mark Cidade

  //C# 2.0
  RemoveAll(delegate(Foo o){ return o.X >= 10; });

or

或者

  //C# 3.0
  RemoveAll(o => o.X >= 10);

or

或者

  Predicate<Foo> matches = delegate(Foo o){ return o.X >= 10; });
  //or Predicate<Foo> matches = o => o.X >= 10;
  RemoveAll(matches);

回答by Nayas Subramanian

Predicate is a delegate which takes an param and returns a boolean.

Predicate 是一个委托,它接受一个参数并返回一个布尔值。

We can do the same in following ways

我们可以通过以下方式做同样的事情

1) Using inline Lambda expression

1)使用内联 Lambda 表达式

RemoveAll(p=> p.x > 2);

2) Using anonymous function

2)使用匿名函数

RemoveAll(delegate(myObject obj){

  return obj.x >=10;
})

3) Using Predicate delegate

3)使用谓词委托

Predicate<myObject> matches = new Predicate<myObject>(IsEmployeeIsValid);
RemoveAll(matches);

Predicate<Foo> matches = delegate(Foo o){ return o.X >= 20; });
RemoveAll(matches);

3) Declaring a delegate explicitily and pointing to a function

3)显式声明委托并指向函数

public delegate bool IsInValidEmployee (Employee emp);

IsInValidEmployee invalidEmployeeDelegate = new IsInValidEmployee(IsEmployeeInValid);
myObjects.RemoveAll(myObject=>invalidEmployeeDelegate(myObject);

// Actual function

// 实际功能

public static bool IsEmployeeInValid(Employee emp)
{
    if (emp.Id > 0 )
        return true;
    else
        return false;
}