Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Action syntax

Tags:

c#

I have two methods like this:

public void ExecuteA()
{
    Write();
    // A specific work
    Clear();
}

public void ExecuteB()
{
    Write();
    // B specific work
    Clear();
}

I want to extract the Write() and Clear() methods to a new method (Action) to have something like this:

public ASpecificWork()
{
    // do A work
}

public BSpecificWork()
{
    // do B work
}

Execute(BSpecificWork);
Execute(ASpecificWork);

The Write() and Clear() will be defined in Execute() just one time. What's the right syntax to do so?

like image 308
Stacked Avatar asked Apr 16 '13 12:04

Stacked


1 Answers

public void Execute(Action action)
{
    Write();
    action();
    Clear();
}
like image 120
Oded Avatar answered Oct 05 '22 08:10

Oded