---
title: "Delegate and Event in C#"  
description: "Delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which c"  
author: "Kenny Tangnde"  
published: 2011-10-29  
updated: 2017-11-29  
canonical: https://www.mindstick.com/articles/777/delegate-and-event-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Delegate and Event in C#

Delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Delegate is type-safe object which can point to method or multiple methods (Multicasting) of application. To define delegate we use keyword ‘Delegate’.\

##### Let’s see an example on ‘Delegate and Event in C#’.

##### Example:

| ``` namespace delegateAndEvent{ public delegate void TimeEventHandler(string s); //declare a delegate public class Mytime { public event TimeEventHandler Timer; //declare a event public void OnTimer(string s) { if (null!=Timer) { Timer(s); //raise the event } } } public class ProcessTime { public void GenerateTime(string s) //event handle { Console.WriteLine("Hello {0}!The time is {1} now",s,DateTime.Now); } } class Program { static void Main(string[] args) { ProcessTime p = new ProcessTime(); Mytime t = new Mytime(); t.Timer += new TimeEventHandler(p.GenerateTime);//To link events and event handling t.OnTimer("Mindstick"); //use event } }} ``` |
| --- |

**Note:** While defining delegate we need to keep in mind that return type, arguments should be same as the return type and arguments of methods to which it is going to point to.

---

Original Source: https://www.mindstick.com/articles/777/delegate-and-event-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
