如何将委托表达式更改为lambda?

Erw*_*ers 0 c# lambda delegates

我正在阅读一些代码并看到以下内容:

Method.Find(delegate(Department depts) { 
     return depts.Id == _departmentId; });
Run Code Online (Sandbox Code Playgroud)

T Find方法具有以下描述:

public T Find(Predicate<T> match);
// Summary:
//     Searches for an element that matches the conditions defined by the specified
//     predicate, and returns the first occurrence within the entire 
//     System.Collections.Generic.List<T>.
//
// Parameters:
//   match:
//     The System.Predicate<T> delegate that defines the conditions of the element
//     to search for.
// (...)
Run Code Online (Sandbox Code Playgroud)

是否有可能重写此方法以将lambda表达式作为参数,如果是,如何?

Ser*_*rvy 5

如果要将lambda表达式传递给它,该方法已经可以接受lambda表达式作为参数.

该方法只是表明它接受委托.有几种方法可以定义委托:

  1. 一个lambda(Find(a => true))
  2. 匿名委托(您在示例中使用的内容)
  3. 方法组 Find(someNamedMethod)
  4. 反思(Find((Predicate<Whatever>)Delegate.CreateDelegate(typeof(SomeClass), someMethodInfo)))