Is it possible to combine to lambdaas which have the same body?

myEntry.Unfocused += async (s, e) =>
                {
                   // Same code
                }

myButton.Clicked += async (s, e) =>
                {
                   // Same code
                }

Is it possible to combine these tow lambdas in order to avoid maintaining the same code twice?

>Solution :

These events are effectively assigning a method to call once invoked. As long as the signature matches, you can point it directly at a named method

myEntry.Unfocused += OnEvent;
myButton.Clicked += OnEvent;

private async Task OnEvent(object sender, EventArgs args)
{
    // Do stuff here
}

Just ensure the return type is correct.

Leave a Reply