Func &Action No more delegates in C#

So Delegates.. What’s the point?

If you haven’t heard of delegates before, that’s OK. Func and Action will help you understand them better as they are cleaner and easier to understand than old fashion delegates.

You can learn about them in my last post here. Basically, they are a pointer or reference to a function instead of a value. They can be very handy in the following scenarios:

  • Creating an event design pattern system.
  • Encapsulating a static method is desired / required.
  • Benchmark testing.

There are several others, but these are some good examples of why you might use them. The last post was based on .NET 1.1+. There have updates that are much easier to use and all around more useful. Generics came along and here is where Func and Action comes in :).

Action’s are superhero’s, but not the Superman Type

The Action type was introduced in .NET 2.0. They are shorthand for generic delegates. Counterparts to a Func type if you will ( we will talk about those next).  The thing about an Action delegate is that it cannot return anything. So it is a void method delegate.

The other interesting part of these Action delegates is that the object passed in is just that, an object. So what does that mean? It means that ANYTHING can be passed to it. Even custom classes. This is because all classes are descendants of type object at compile time.

As usual, I like to provide an example:

Normally, you might create a delegate using the following syntax.

This delegate expects a string of some value to be passed in and should not return anything. Now for the new syntax with Action.

So this now simplifies the code a bit. Instead of explicitly defining a new delegate with a specific method name, you can use this Action syntax.

There are some new things you might notice about this syntax . It doesn’t show the returning of the string and this <> stuff. This is called implicit declaration. What this means is whatever type you define in the <>, is what the parameter type is. To learn more, look up generics.

To use the Action delegate, there are 2 options. The first is the conventional method syntax.

The second option is to use anonymous methods syntax.

If you have ever used the ForEach  method here defined , you will notice it takes in a type of Action<T>. This is because it is used to loop through some set of actions and not return anything.

What is Func? When someone doesn’t take a shower?

No, of course not. Func is short hand for a delegate that takes 0 or more parameters and returns some result. This delegate shorthand extends the Action<T> by allowing both parameters and return types. The cool thing about Func<T> is that you can use that implicit declaration again. This again is using Generics. Now that I’m talking about it so much, I might do another post on those too.

So what does a Func<T> delegate look like you say? Well, ask and you shall recieve.

So there you go. Func<T> uses the syntax Func<bool> to define that the out parameter is a bool without passing a parameter definition name.

So what if we did want to pass in something different than the return type

Glad you asked! Luckily, Microsoft did have the foresight to plan for this. Both Func and Action have overloads that have multiple parameters. So what does this look like?

Action multiple parameter example

Action has Action<T1,T2,T3,T4>. So it can take up to 3 parameters.

The above example is what an anonymous version of string concatenation might look like. We might use it like this

Func multiple parameter example

Func also has Func<T1,T2,T3,T4>. So it can take up to 3 parameters.

Next time, we will go over generics and anonymous functions in more detail to see how we can simplify things even more.

 

Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *