Delegate vs Action vs Func

Traditional way of using Delegate

When we talk about delegate, it is a class of reference type used to encapsulate named or anonymous methods. For creating delegate we write,

Delegate

Now, using the delegate which is declared can be done as shown below:

Delegate in c sharp

Now C# had added a small wrapper to the delegate with the help of Action<in T> delegate.

Action<T>

This delegate was introduced in Framework 2.0. We can now use Action<in T> delegate to pass a method as a parameter without explicitly defining the delegate. The compiler will take care of it. This delegate can accept parameters but without a return type.

define the delegate

Call the delegate using act(4, 5)

Func< T, TResult>

This was introduced in Framework3.5. This delegate is different from Action<T> in the sense that it supports for return value.

return value

To call this delegate we can use

Console.WriteLine(“Using Func<> :” + fn(6, 6));

As it is returning the value. Hope you got the difference.