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#

Delegates and events in C#

  • 1.
    Delegates and Events inC# Dr. Neeraj Kumar Pandey https://www.slideshare.net/nkpandey
  • 2.
    Delegates A delegate isan 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  Adelegate 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 methodswhose 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# providesspecial 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 adelegate 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 SingleCast Delegates. Multi Cast Delegates. DelegatesandEventsinC#
  • 8.
  • 9.
    Multicast Delegate Multicast delegatecan 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#
  • 10.
  • 11.
    Multicast Delegate If Dis 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 isa 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#
  • 13.
  • 14.
    Reference: • E. Balaguruswami“Programming in C#” DelegatesandEventsinC#