Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Action/Func better than a regular Method in .Net?

Tags:

c#

.net

action

func

I much prefer using an Action or a Func if I need a quick reusable piece of code, however others on my team don't like them or understand them.
At the moment my only real argument is about preference and more up to date code practices, but those are just bad arguments.

Why is it better to do this:

Action<FormView,String> hideControl = (form,name) => {
    var button = form.GetControl<Button>(name);
    if (button != null)
        button.Visible = false;
}

than:

public static void HideControl<T>(this FormView form, string name) where T : Control
{
    var button = form.GetControl<Button>(name);
    if (button != null)
        button.Visible = false;
}

?

Can anyone give me solid concrete arguments/examples?

like image 703
James P. Wright Avatar asked Dec 09 '11 17:12

James P. Wright


People also ask

What is the difference between action and func?

Action is a delegate (pointer) to a method, that takes zero, one or more input parameters, but does not return anything. Func is a delegate (pointer) to a method, that takes zero, one or more input parameters, and returns a value (or reference).

What are Func and Action Why do we use C#?

The Func and Action generic delegates were introduced in the . NET Framework version 3.5. Whenever we want to use delegates in our examples or applications, typically we use the following procedure: Define a custom delegate that matches the format of the method.

What is the point of func in C#?

Func is a delegate that points to a method that accepts one or more arguments and returns a value. Action is a delegate that points to a method which in turn accepts one or more arguments but returns no value. In other words, you should use Action when your delegate points to a method that returns void.

What are the differences between Func Predicate action?

The Func delegate takes zero, one or more input parameters, and returns a value (with its out parameter). Action takes zero, one or more input parameters, but does not return anything. Predicate is a special kind of Func.


2 Answers

There are two sides to your question: style and performance.

For code style, using delegates makes things a little messy. There is no function name associated with the body of code (anonymous method), argument types are inferred, and when overused this can lead to large function blocks containing lots of smaller independent chunks of code in delegates. It's not pretty to look at, it can be hard to figure out what the code is actually doing, and it's hard to find what you're looking for.

For performance, delegates introduce an indirection that is not as fast to execute as a plain old method. The difference is small, but when used to excess it could become noticeable.

Any tool has appropriate and excessive uses. It's appropriate to use Action or Func for a callback parameter when you want to pass a callback routine into a function. Using Action or Func as a replacement for declaring functions in general is a misuse, IMO.

like image 177
dthorpe Avatar answered Nov 10 '22 01:11

dthorpe


The short answer is that, in general, it is not better. I agree with your team mates.

I don't think your question is the right one. Rather than asking why is it better (believe me it is not in most cases), you might ask WHEN is it better.

Now, there are of course times where they are extremely useful, like in LINQ when methods take in a Func, writing a quick lambda is great.

This is far more readable and maintainable:

var myQuery = Customers.Where(x => x.ID == 3).First();

than the alternative:

public bool FilterByID3(Customer cust)
{
  return cust.ID == 3;
}

    var myQuery = Customers.Where(FilterByID3).First();

In most other cases, it is much more readable/maintainable to use functions.

like image 22
John Buchanan Avatar answered Nov 10 '22 00:11

John Buchanan