SlideShare a Scribd company logo
Delegates and Events
in C#
Dr. Neeraj Kumar Pandey
https://www.slideshare.net/nkpandey
Delegates
A delegate is an object that can refer to a method.
Therefore, when we create a delegate, we are creating an object
that can hold a reference to a method.
Creating and using delegates involves four steps. They include:-
• Delegate Declaration
• Delegate method definition
• Delegate instantiation
• Delegate invocation.
DelegatesandEventsinC#
Delegate Declaration
 A delegate declaration is a type declaration and takes the following form:-
modifier delegate return-type delegate-name (parameters);
 delegate is the keyword that signifies that the declaration represents a
class type derived from System.Delegate.
 modifier used with delegates are:- new, internal,public,private,protected.
For eg:-
delegate void SimpleDelegate();
delegate int Mathoperation(int x, int y);
public delegate int Compareitems(object obj1, object obj2);
 Delegate types are implicitly sealed.
DelegatesandEventsinC#
Delegate Methods
The methods whose references are encapsulated into a delegate
instances are known as delegate methods or callable entities.
The signature and return type of delegate methods must exactly
match the signature and return type of the delegate.
For eg:-
delegate void Delegate1();
can encapsulate to the following methods.
public void F1()
{
Console.WriteLine(“F1”);
}
static void F2(){}
DelegatesandEventsinC#
Delegate Instantiation
C# provides special syntax for instantiating their instances.
new delegate-type(expression);
delegate int productdelegate(int x, int y);//delegate declaration
static int Product(int a, int b)//delegate method
{
return (a*b);
}
productdelegate p =new productdelegate(expression);
DelegatesandEventsinC#
Delegate Invocation
When a delegate is invoked, it in turn invokes the method whose
reference has been encapsulated into the delegate.
delegate_object(parameters list);
for eg:-
delegate1(x,y);
double result=delegate(4.5,5.6);
DelegatesandEventsinC#
Types of Delegates
Single Cast Delegates.
Multi Cast Delegates.
DelegatesandEventsinC#
Implementing
SingleCast
Delegate
DelegatesandEventsinC#
Multicast Delegate
Multicast delegate can be used to invoke the multiple methods.
The delegate instance can do multicasting (adding new method on
existing delegate instance) using the + operator and – operator can
be used to remove a method from a delegate instance. All methods
will invoke in sequence as they are assigned.
Multicast delegates, also known as combinable delegates.
They must follow following conditions.
• The return type must be void
• None of the parameters of the delegate type can be declared as
output parameter.
DelegatesandEventsinC#
Multicast Delegate
DelegatesandEventsinC#
Multicast Delegate
If D is a delegate that satisfies the above conditions and d1,d2,d3 and
d4 are the instances of D, then the statements.
d3=d1+d2;//d3 refers to two methods.
d4=d3-d2;//d4 refers to only d1 method.
Delegates are invoked in the order they are added.
DelegatesandEventsinC#
Events
An event is a delegate type class member that is used by the object
or class to provide a notification to other objects that an event has
occurred.
Events are declared using the simple event declaration format as
follows:-
modifier event type event-name;
The modifier may be a new , static , override , abstract and sealed.
For eg:-
public event EventHandler Click;
EventHandler is a delegate and Click is an event.
DelegatesandEventsinC#
Events
DelegatesandEventsinC#
Reference:
• E. Balaguruswami “Programming in C#”
DelegatesandEventsinC#
Ad

More Related Content

What's hot (20)

Explain Delegates step by step.
Explain Delegates step by step.Explain Delegates step by step.
Explain Delegates step by step.
Questpond
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
Raghuveer Guthikonda
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
Operators in java
Operators in javaOperators in java
Operators in java
AbhishekMondal42
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
Google
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
Prem Kumar Badri
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
LINQ in C#
LINQ in C#LINQ in C#
LINQ in C#
Basant Medhat
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
Lovely Professional University
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
Information Technology
 
Delegetes in c#
Delegetes in c#Delegetes in c#
Delegetes in c#
Mahbub Hasan
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Java thread life cycle
Java thread life cycleJava thread life cycle
Java thread life cycle
Archana Gopinath
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
Minal Maniar
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 

Similar to Delegates and events in C# (20)

Delegates and events
Delegates and events   Delegates and events
Delegates and events
Gayathri Ganesh
 
Delegates and events
Delegates and eventsDelegates and events
Delegates and events
Iblesoft
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_ii
Nico Ludwig
 
30csharp
30csharp30csharp
30csharp
Sireesh K
 
30c
30c30c
30c
Sireesh K
 
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAMPROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
SaraswathiRamalingam
 
Delegates in C#
Delegates in C#Delegates in C#
Delegates in C#
SNJ Chaudhary
 
Interface
InterfaceInterface
Interface
Ashwini Yadav
 
Application package
Application packageApplication package
Application package
JAYAARC
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & Methods
Eng Teong Cheah
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
dplunkett
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptx
KunalYadav65140
 
JAVA-PPT'S.pptx
JAVA-PPT'S.pptxJAVA-PPT'S.pptx
JAVA-PPT'S.pptx
RaazIndia
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
RAJASEKHARV10
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
AnushaNaidu
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
Pranali Chaudhari
 
Clean code
Clean codeClean code
Clean code
Khou Suylong
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
Unit2_2.pptx Chapter 2 Introduction to C#
Unit2_2.pptx Chapter 2 Introduction to C#Unit2_2.pptx Chapter 2 Introduction to C#
Unit2_2.pptx Chapter 2 Introduction to C#
Priyanka Jadhav
 
Toulouse.pptx presentación presentación presentación
Toulouse.pptx presentación presentación presentaciónToulouse.pptx presentación presentación presentación
Toulouse.pptx presentación presentación presentación
Diego Martín
 
Delegates and events
Delegates and eventsDelegates and events
Delegates and events
Iblesoft
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_ii
Nico Ludwig
 
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAMPROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
SaraswathiRamalingam
 
Application package
Application packageApplication package
Application package
JAYAARC
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & Methods
Eng Teong Cheah
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
dplunkett
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptx
KunalYadav65140
 
JAVA-PPT'S.pptx
JAVA-PPT'S.pptxJAVA-PPT'S.pptx
JAVA-PPT'S.pptx
RaazIndia
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
RAJASEKHARV10
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
Unit2_2.pptx Chapter 2 Introduction to C#
Unit2_2.pptx Chapter 2 Introduction to C#Unit2_2.pptx Chapter 2 Introduction to C#
Unit2_2.pptx Chapter 2 Introduction to C#
Priyanka Jadhav
 
Toulouse.pptx presentación presentación presentación
Toulouse.pptx presentación presentación presentaciónToulouse.pptx presentación presentación presentación
Toulouse.pptx presentación presentación presentación
Diego Martín
 
Ad

More from Dr.Neeraj Kumar Pandey (19)

Structure in c#
Structure in c#Structure in c#
Structure in c#
Dr.Neeraj Kumar Pandey
 
Strings in c#
Strings in c#Strings in c#
Strings in c#
Dr.Neeraj Kumar Pandey
 
Program control statements in c#
Program control statements in c#Program control statements in c#
Program control statements in c#
Dr.Neeraj Kumar Pandey
 
Operators and expression in c#
Operators and expression in c#Operators and expression in c#
Operators and expression in c#
Dr.Neeraj Kumar Pandey
 
Method parameters in c#
Method parameters in c#Method parameters in c#
Method parameters in c#
Dr.Neeraj Kumar Pandey
 
Enumeration in c#
Enumeration in c#Enumeration in c#
Enumeration in c#
Dr.Neeraj Kumar Pandey
 
Dot net assembly
Dot net assemblyDot net assembly
Dot net assembly
Dr.Neeraj Kumar Pandey
 
Cloud introduction
Cloud introductionCloud introduction
Cloud introduction
Dr.Neeraj Kumar Pandey
 
Role of cloud computing in scm
Role of cloud computing in scmRole of cloud computing in scm
Role of cloud computing in scm
Dr.Neeraj Kumar Pandey
 
Public cloud
Public cloudPublic cloud
Public cloud
Dr.Neeraj Kumar Pandey
 
cloud computing Multi cloud
cloud computing Multi cloudcloud computing Multi cloud
cloud computing Multi cloud
Dr.Neeraj Kumar Pandey
 
Ibm bluemix case study
Ibm bluemix case studyIbm bluemix case study
Ibm bluemix case study
Dr.Neeraj Kumar Pandey
 
Business cases for the need of cloud computing
Business cases for the need of cloud computingBusiness cases for the need of cloud computing
Business cases for the need of cloud computing
Dr.Neeraj Kumar Pandey
 
cloud computing:Types of virtualization
cloud computing:Types of virtualizationcloud computing:Types of virtualization
cloud computing:Types of virtualization
Dr.Neeraj Kumar Pandey
 
cloud computing: Vm migration
cloud computing: Vm migrationcloud computing: Vm migration
cloud computing: Vm migration
Dr.Neeraj Kumar Pandey
 
Cloud Computing: Virtualization
Cloud Computing: VirtualizationCloud Computing: Virtualization
Cloud Computing: Virtualization
Dr.Neeraj Kumar Pandey
 
Dot net introduction
Dot net introductionDot net introduction
Dot net introduction
Dr.Neeraj Kumar Pandey
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#
Dr.Neeraj Kumar Pandey
 
C# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net FrameworkC# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net Framework
Dr.Neeraj Kumar Pandey
 
Ad

Recently uploaded (20)

HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 

Delegates and events in C#

  • 1. Delegates and Events in C# Dr. Neeraj Kumar Pandey https://www.slideshare.net/nkpandey
  • 2. Delegates A delegate is an object that can refer to a method. Therefore, when we create a delegate, we are creating an object that can hold a reference to a method. Creating and using delegates involves four steps. They include:- • Delegate Declaration • Delegate method definition • Delegate instantiation • Delegate invocation. DelegatesandEventsinC#
  • 3. Delegate Declaration  A delegate declaration is a type declaration and takes the following form:- modifier delegate return-type delegate-name (parameters);  delegate is the keyword that signifies that the declaration represents a class type derived from System.Delegate.  modifier used with delegates are:- new, internal,public,private,protected. For eg:- delegate void SimpleDelegate(); delegate int Mathoperation(int x, int y); public delegate int Compareitems(object obj1, object obj2);  Delegate types are implicitly sealed. DelegatesandEventsinC#
  • 4. Delegate Methods The methods whose references are encapsulated into a delegate instances are known as delegate methods or callable entities. The signature and return type of delegate methods must exactly match the signature and return type of the delegate. For eg:- delegate void Delegate1(); can encapsulate to the following methods. public void F1() { Console.WriteLine(“F1”); } static void F2(){} DelegatesandEventsinC#
  • 5. Delegate Instantiation C# provides special syntax for instantiating their instances. new delegate-type(expression); delegate int productdelegate(int x, int y);//delegate declaration static int Product(int a, int b)//delegate method { return (a*b); } productdelegate p =new productdelegate(expression); DelegatesandEventsinC#
  • 6. Delegate Invocation When a delegate is invoked, it in turn invokes the method whose reference has been encapsulated into the delegate. delegate_object(parameters list); for eg:- delegate1(x,y); double result=delegate(4.5,5.6); DelegatesandEventsinC#
  • 7. Types of Delegates Single Cast Delegates. Multi Cast Delegates. DelegatesandEventsinC#
  • 9. Multicast Delegate Multicast delegate can be used to invoke the multiple methods. The delegate instance can do multicasting (adding new method on existing delegate instance) using the + operator and – operator can be used to remove a method from a delegate instance. All methods will invoke in sequence as they are assigned. Multicast delegates, also known as combinable delegates. They must follow following conditions. • The return type must be void • None of the parameters of the delegate type can be declared as output parameter. DelegatesandEventsinC#
  • 11. Multicast Delegate If D is a delegate that satisfies the above conditions and d1,d2,d3 and d4 are the instances of D, then the statements. d3=d1+d2;//d3 refers to two methods. d4=d3-d2;//d4 refers to only d1 method. Delegates are invoked in the order they are added. DelegatesandEventsinC#
  • 12. Events An event is a delegate type class member that is used by the object or class to provide a notification to other objects that an event has occurred. Events are declared using the simple event declaration format as follows:- modifier event type event-name; The modifier may be a new , static , override , abstract and sealed. For eg:- public event EventHandler Click; EventHandler is a delegate and Click is an event. DelegatesandEventsinC#
  • 14. Reference: • E. Balaguruswami “Programming in C#” DelegatesandEventsinC#