如何将Action提取到成员函数中?

sha*_*oth 2 .net c# linq lambda

我有这个代码:

class SomeClass {
    void someFunction()
    {
          Action<string> myAction = (what)=>
          {
             //whatever
          }
          new List<string>().ForEach(myAction);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想将代码内部提取myAction到一个单独的成员函数中.

我怎么做?

Dar*_*rov 7

class SomeClass
{
    void someFunction()
    {
        Action<string> myAction = Whatever;
        new List<string>().ForEach(myAction);
    }

    public void Whatever(string what)
    {
        // ... whenever
    }
}
Run Code Online (Sandbox Code Playgroud)

或直接,无需定义局部Action<string>变量(无论如何都可能在Release模式中进行优化):

new List<string>().ForEach(Whatever);
Run Code Online (Sandbox Code Playgroud)