Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract an Action into a member function?

Tags:

c#

.net

lambda

linq

I have this code:

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

I'd like to extract the code inside myAction into a separate member function.

How do I do that?

like image 955
sharptooth Avatar asked Feb 13 '26 05:02

sharptooth


1 Answers

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

    public void Whatever(string what)
    {
        // ... whenever
    }
}

or directly, without defining a local Action<string> variable (that will probably be optimized away in Release mode anyway):

new List<string>().ForEach(Whatever);
like image 173
Darin Dimitrov Avatar answered Feb 15 '26 13:02

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!