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,

1

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

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.

3

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.

4

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.

Advertisements

Process & Thread

A process can be any executing instance which executes under operating system. A process execution is isolated from each other. They consume their own memory space. A Process can have multiple threads. Threads are small task which run under a single process so they have limited level of isolation.Once any process is killed, then the all threads under that process also get killed.