你如何声明内联谓词代表?

时间:2022-09-07 11:08:48

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 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的委托,但我想知道如何使用匿名委托定义这个内联,而不是创建一堆仅在一次使用的委托函数。

4 个解决方案

#1


53  

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构造:)

#2


15  

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不支持匿名委托。

#3


10  

  //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);

#4


0  

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;
}

#1


53  

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构造:)

#2


15  

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不支持匿名委托。

#3


10  

  //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);

#4


0  

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;
}