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#

More Related Content

What's hot

JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
Delegates in C#
Delegates in C#Delegates in C#
Delegates in C#
SNJ Chaudhary
 
Java package
Java packageJava package
Java package
CS_GDRCST
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
Google
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
javaicon
 
Delegates and events
Delegates and events   Delegates and events
Delegates and events
Gayathri Ganesh
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
Prem Kumar Badri
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
Nahian Ahmed
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Java swing
Java swingJava swing
Java swing
Apurbo Datta
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
Harshal Misalkar
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha
 
Java IO
Java IOJava IO
Java IO
UTSAB NEUPANE
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
Boopathi K
 

What's hot (20)

JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Delegates in C#
Delegates in C#Delegates in C#
Delegates in C#
 
Java package
Java packageJava package
Java package
 
Java threads
Java threadsJava threads
Java threads
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
 
Delegates and events
Delegates and events   Delegates and events
Delegates and events
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
interface in c#
interface in c#interface in c#
interface in c#
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Java swing
Java swingJava swing
Java swing
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Java IO
Java IOJava IO
Java IO
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 

Similar to Delegates and events in C#

Delegates and events
Delegates and eventsDelegates and events
Delegates and eventsIblesoft
 
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
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 packageJAYAARC
 
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 Chpt4dplunkett
 
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
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4thConnex
 
C sharp chap5
C sharp chap5C sharp chap5
C sharp chap5
Mukesh Tekwani
 
Understanding Implicits in Scala
Understanding Implicits in ScalaUnderstanding Implicits in Scala
Understanding Implicits in Scala
datamantra
 
Diifeerences In C#
Diifeerences In C#Diifeerences In C#
Diifeerences In C#
rohit_gupta_mrt
 
Delegate
DelegateDelegate
Delegate
rsvermacdac
 

Similar to Delegates and events in C# (20)

Delegates and events
Delegates and eventsDelegates and events
Delegates and events
 
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
 
30csharp
30csharp30csharp
30csharp
 
30c
30c30c
30c
 
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAMPROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
 
Interface
InterfaceInterface
Interface
 
Application package
Application packageApplication package
Application package
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & Methods
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptx
 
JAVA-PPT'S.pptx
JAVA-PPT'S.pptxJAVA-PPT'S.pptx
JAVA-PPT'S.pptx
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Clean code
Clean codeClean code
Clean code
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
 
C sharp chap5
C sharp chap5C sharp chap5
C sharp chap5
 
Understanding Implicits in Scala
Understanding Implicits in ScalaUnderstanding Implicits in Scala
Understanding Implicits in Scala
 
Diifeerences In C#
Diifeerences In C#Diifeerences In C#
Diifeerences In C#
 
Delegate
DelegateDelegate
Delegate
 

More from Dr.Neeraj Kumar Pandey

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
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
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
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
 

More from Dr.Neeraj Kumar Pandey (20)

Structure in c#
Structure in c#Structure in c#
Structure in c#
 
Strings in c#
Strings in c#Strings in c#
Strings in c#
 
Program control statements in c#
Program control statements in c#Program control statements in c#
Program control statements in c#
 
Operators and expression in c#
Operators and expression in c#Operators and expression in c#
Operators and expression in c#
 
Method parameters in c#
Method parameters in c#Method parameters in c#
Method parameters in c#
 
Enumeration in c#
Enumeration in c#Enumeration in c#
Enumeration in c#
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Dot net assembly
Dot net assemblyDot net assembly
Dot net assembly
 
Cloud introduction
Cloud introductionCloud introduction
Cloud introduction
 
Role of cloud computing in scm
Role of cloud computing in scmRole of cloud computing in scm
Role of cloud computing in scm
 
Public cloud
Public cloudPublic cloud
Public cloud
 
cloud computing Multi cloud
cloud computing Multi cloudcloud computing Multi cloud
cloud computing Multi cloud
 
Ibm bluemix case study
Ibm bluemix case studyIbm bluemix case study
Ibm bluemix case study
 
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
 
cloud computing:Types of virtualization
cloud computing:Types of virtualizationcloud computing:Types of virtualization
cloud computing:Types of virtualization
 
cloud computing: Vm migration
cloud computing: Vm migrationcloud computing: Vm migration
cloud computing: Vm migration
 
Cloud Computing: Virtualization
Cloud Computing: VirtualizationCloud Computing: Virtualization
Cloud Computing: Virtualization
 
Dot net introduction
Dot net introductionDot net introduction
Dot net introduction
 
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#
 
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
 

Recently uploaded

The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 

Recently uploaded (20)

The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 

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#