Event vs Delegate

By | January 11, 2018

Event and Delegate are interrelated. In this post, we will learn how they are different each other.

An Event declaration adds a layer of abstraction and protection on the delegate instance. This protection prevents clients of the delegate from resetting the delegate and its invocation list and only allows adding or removing targets from the invocation list.

 

Events
Delegates
In your application, you might want an action to be performed on click of a button. This button click is an event. Likewise you might define any number of events as per your need. When an event fires, an appropriate action has to be taken. This action is triggered with the help of delegates.
An Event can trigger a set of delegates each executing a different method. Delegates can wrap and execute only the methods which have the same signature as that of the delegates.
The keyword Event is a modifier. The keyword delegate refers to a type which in turn is used as a method
reference.
Events contain two accessor methods namely add method and remove method. You can override them if required. No such accessor methods are associated with delegate.
Access to events is very restricted when compared to delegates. Even if you mark an event with public modifier, other classes can only perform two activities on that event: adding an event handler and removing
an event handler. Other classes are not allowed to fire the event or trace out the existing handlers of that event.
Access to delegates is based on what access modifier is associated with it. If the delegate is public then it can be used to wrap methods of any classes in any project which match its signature.

You can also learn Delegate in depth by clicking here.

In below example, a simple event created and triggered using a newly created delegate:

using System;

namespace ConsoleApp1
{
    public delegate void testDelegate();
    public class eventClass
    {
        public event testDelegate testEvent;
        public void triggerEvent()
        {
            if (testEvent != null)
            {
                testEvent();
            }
        }
    }
    public class testClass
    {
        private static void message()
        {
            Console.WriteLine("Event Triggered");
            Console.ReadLine();
        }
        public static void Main()
        {
            eventClass obj = new eventClass();
            obj.testEvent += new testDelegate(message);
            obj.triggerEvent();
        }
    }
}

Output:
Event Triggered

In below example of delegates which wrap an instance method and static method sharing the same signature as that of the delegate:

using System;

namespace ConsoleApp1
{
public delegate void testDelegate(int data);
public class sampleClass
{
public static void staticMethod(int data)
{
Console.WriteLine("In staticMethod:{0} ", data);
}
public void instanceMethod(int data)
{
Console.WriteLine("In instanceMethod:{0}", data);
Console.ReadLine();
}
}
public class testClass
{
public static void Main()
{
sampleClass obj = new sampleClass();
testDelegate delObj = new testDelegate(sampleClass.staticMethod);
delObj += new testDelegate(obj.instanceMethod);
delObj(30);
}
}
}

Output:
In staticMethod:30
In instanceMethod:30

I hope you will enjoy the difference between Event and Delegate in c#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Like it? Share it