HatSoft HatSoft - 1 year ago 79
C# Question

What is the difference between Delegate & Action in C#

If I searched I will definitely get examples of showing what is a Delegate and Action.
I have read the basic books giving examples of delegate.

But what I want to know is their use in the real world. Please do not give Hello World example or the Animal class they are far too basic

For e.g. :

  1. What are the difference between them

  2. When to use a delegate or action

  3. When not to use a delegate or action

  4. When they can be an over kill

Answer Source

Action is a Delegate. It is defined like this:

public delegate void Action();

You could create your own delegate types similarly to how you would create abstract methods; you write the signature but no implementation. You then create instances of these delegates by taking the reference of a method.

class Program
    public static void FooMethod()
        Console.WriteLine("Called foo");

    static void Main()
        Action foo = FooMethod; // foo now references FooMethod()
        foo(); // outputs "Called foo"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download