DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Event Driven Architecture (EDA) - Optimizer or Complicator
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Streamlining Event Data in Event-Driven Ansible

Trending

  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  • Data Quality: A Novel Perspective for 2025
  • Unlocking AI Coding Assistants: Generate Unit Tests
  • Agile’s Quarter-Century Crisis

Event vs. Delegate

Explaining the important differences between the Event and Delegate patterns in C# and why they're useful.

By 
Pranay Rana user avatar
Pranay Rana
·
Jul. 20, 16 · Tutorial
Likes (13)
Comment
Save
Tweet
Share
60.1K Views

Join the DZone community and get the full member experience.

Join For Free

This post explains the basics of Events and Delegate in .NET and, most importantly, explains what is difference between both. 

Both Event and Delegate is based on overserved pattern of GOF, which is based on giving notification of change in one thing (object) to all others who is interested in change. Below image is graphical interpretation of same.


Delegates 

Delegate in C# is a reference type, which holds a reference to the function and invokes the function when called with an Invoke method. If one is coming from C++/C background, delegate is like a pointer to a function. 

To understand delegates in a better way, take a look at the below sample code:

public class DelegateTest {     
    public delegate void Print(string val);       
    public DelegateTest()     {         
      Print p = new Print(PrintValue);         
      p += new Print(PrintData);         
      p.Invoke("Test");    
      }       

    private void PrintData(string s)     {         
      Console.WriteLine("PrintData" + s);     
      }       

    public void PrintValue(string s)     
    {         
      Console.WriteLine("PrintValue" + s);     
      }
    }

When above class get invoked following output will get printed: 

PrintData Test 

PrintValue Test 

Some key points about the above code:

  1. It defines Delegate, which points to a method with the void return type and takes a string as an input.
  2. In the constructor of a type, it creates a reference object of Delegate which points to the methods “PrintData” and “PrintValue”.
  3. Invokes a method of delegate, which calls both methods one by one.

Read more about Delegates here: https://msdn.microsoft.com/en-in/library/900fyy8e(v=vs.71).aspx

Events 

Event in C# is a type of Delegate, which means that if one wants to use Event, then one must define delegate first. Events can have multiple event-handler functions, which have a signature like Delegate, and get called when an Event is raised by some other Event in an application. A simple example is when a button is clicked in the UI. Events are very useful to create notifications. 

To understand Events in a better way, take a look at the below sample code:

public class EventTest {     
    public delegate void Print(string val);     
    public event Print PrintEvent;   

    public EventTest()     {         
      this.PrintEvent +=  PrintData;        
      this.PrintEvent += PrintValue;     
      }       

    public virtual void OnPrintEvent()     {         
      if (PrintEvent != null)             
        PrintEvent("Test");     
        }       

      private void PrintData(string s)     {         
        Console.WriteLine("PrintData" + s);     
        }       
      public void PrintValue(string s)     
      {         
        Console.WriteLine("PrintValue" + s);     } 
      }


When above class object is created and then “OnPrintEvent” method invoked following output will get printed:

PrintData Test 

PrintValue Test 

Some key points in the above code:

  1. Its creates a Delegate, which can point to a method with return type void and take a string as an argument.
  2. It defines Event, which is defined as a Delegate.
  3. In Constructor, an event holds two delegates, i.e. Event handlers. It can also be written as:
this.PrintEvent += new Print (PrintData);
this.PrintEvent += new Print (PrintValue);


  1. OnPrintEvent method checks that the Event is holding an Event handler function or not, if there are any Event handler(s) it calls all Event handlers.

Read more about Events: https://msdn.microsoft.com/en-in/library/8627sbea%28v=vs.71%29.aspx 

Events vs. Delegates 

Both Event and Delegate do the same task, which is to hold event handlers and call them when delegate or event are invoked. So we have a question: Why do you need Event in a language like C# when one can achieve the same thing with Delegate?

The answer is that Event is a wrapper on the Delegate type. 

The Problem With Delegate 

Let’s understand this, using the same class defined above which is “DelegateTest”.

class Program {       

    static void Main(string[] args)     {         
      Program p = new Program();         
      DelegateTest delegateTest = new DelegateTest();         
      delegateTest.p = new DelegateTest.Print(p.TestString);        
      delegateTest.p = null;     }       
    public void TestString(string s)    
    {     }
    }

The above code performs the following steps:

  1. It creates an object of the DelegateTest type.
  2. Its assigns new a Delegate reference to Delegate Print and overrides the value assigned in the constructor.
  3. It assigns “null” to the Print Delegate and removes all added functions.

The Problem with Delegate is that one can easily override Delegate properties, and that leads to error or serious issue. That means one cannot use Delegate as a public property. 

The Solution With Event

To avoid the above problem, C# uses Events, which defines wrappers around Delegates. The below code makes use of the EventTest class defined above, and PrintEvent is a wrapper around the Print Delegate. 

static void Main(string[] args)         
{             
  Program p = new Program();            
  EventTest eventTest = new EventTest();             
  //eventTest.PrintEvent = null;
  //Not allowed in C#             
  eventTest.PrintEvent += p.TestString;         
  }          
public void TestString(string s)         
{         }

The above code performs the following steps: 

  1. It defines the object EventTest.
  2. its assigns eventhandler to the PrintEvent Event of EventTest.

One cannot do this with Event:

eventTest.PrintEvent = null;//Not allowed in C# 
eventTest.PrintEvent = new PrintEvent()//Not allowed in C#

So the Event type resolves the problem of exposing Delegates outside of a class by defining wrappers around Delegate. The below image is a representation of Event and Delegate.

More differences between Event and Delegate are: 

  1. Event is very helpful to create Notification systems. The same is not possible with Delegate because Delegate cannot be exposed as public.
  2. Delegate is very helpful to create call back functions, i.e pass delegate as function argument, which is not possible with Event.

Conclusion 

Event and Delegate both follow the Observer pattern. The difference between both is that Event wraps Delegate type, and makes Delegate not modifiable in terms of changing references, i.e. assigning a new object is not possible.

Event

Published at DZone with permission of Pranay Rana, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Event Driven Architecture (EDA) - Optimizer or Complicator
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Streamlining Event Data in Event-Driven Ansible

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: