c# - Passing anonymous functions instead of hooking up events

Viewed 322

I have recently discovered the use of Actions and anonymous functions and how they can be handy when working with tasks. I have done a couple of tests, passing anonymous functions into a class to work similarly to an event.

An example of what Im doing would be...

var myForm = new MyForm();
myForm.OnCertainEvent(output => {
    //interact with UI based on feedback.
}));

Then in the form itself, I would set an Action property with the Action passed into the OnCertainEvent method.

I think this syntax is very clean instead of declaring events, delegates, and hooking them up with the += syntax.

Would this be bad practice to do? Obviously it isn't traditional, but does it have any negative effects? Essentially I'm just providing a callback.

3 Answers
Related