0% found this document useful (0 votes)
83 views17 pages

Delegates

Delegates allow methods to be passed as arguments to other methods. They provide a way to encapsulate and invoke methods similarly to function pointers in other languages. There are two types of delegates - singlecast which point to a single method, and multicast which can point to multiple methods and invoke them in FIFO order. Events use delegates to allow classes to notify subscribers when something of interest occurs by attaching event handler methods to the delegate.

Uploaded by

f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views17 pages

Delegates

Delegates allow methods to be passed as arguments to other methods. They provide a way to encapsulate and invoke methods similarly to function pointers in other languages. There are two types of delegates - singlecast which point to a single method, and multicast which can point to multiple methods and invoke them in FIFO order. Events use delegates to allow classes to notify subscribers when something of interest occurs by attaching event handler methods to the delegate.

Uploaded by

f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Delegates

What is Delegates ?
• A delegate is an object which refers to a method or you can say it is a
reference type variable that can hold a reference to the methods.

• It works like function pointer in C and C++.

• But it is objected-oriented, secured and type-safe than function pointer.

• Delegate is of reference type.

• Provides a good way to encapsulate the methods.

• Delegates are the library class in System namespace.


• Delegate signature should be same as the method
signature.

• Delegate can point to a parameterized method or


non-parameterized method.

• Delegate has no implementation means no Body


with{}.

• We can use Invoke() method with delegates.


Types of delegates
• There are two types of delegates, singlecast delegates, and
multiplecast delegates.

• Singlecast delegate
– Singlecast delegate point to single method at a time.

– In this the delegate is assigned to a single method at a time. They are


derived from System.Delegate class.
 
• Multicast Delegate
– When a delegate is wrapped with more than one method that is known
as a multicast delegate.
Declaration of Delegates
• Delegate type can be declared using the delegate
keyword.

• Once a delegate is declared, delegate instance will


refer and call those methods whose return type and
parameter-list matches with the delegate declaration.

• Syntax:
– [modifier] delegate [return_type] [delegate_name]
([parameter_list]);
• modifier: It is the required modifier which defines the access
of delegate and it is optional to use.

• delegate: It is the keyword which is used to define the


delegate.

• return_type: It is the type of value returned by the methods


which the delegate will be going to call. It can be void. A
method must have the same return type as the delegate.

• delegate_name: It is the user-defined name or identifier for


the delegate.

• parameter_list: This contains the parameters which are


required by the method when called through the delegate.
Instantiation of Delegates
• After declaring a delegate, a delegate object is created
with the help of new keyword.

• Once a delegate is instantiated, a method call made to


the delegate is pass by the delegate to that method.

• Syntax
– [delegate_name] [instance_name] = new [delegate_name]
(calling_method_name);
Example Single Delegate
using System; public static int getNumber()
//declaring delegate {
delegate int Calculator(int n); return number;
public class DelegateExample }
{ public static void Main()
static int number = 100; { // instantiating delegate
Calculator c1 = new Calculator(add);
public static int add(int n)
Calculator c2 = new Calculator(mul);
{
/ /calling method using delegate
number = number + n; c1(20);
return number; Console.WriteLine("After c1 delegate, Number
} is: " + getNumber());
public static int mul(int n) c2(3);
Console.WriteLine("After c2 delegate, Number
{
is: " + getNumber());
number = number * n;
return number; }
} }
Multicasting of a Delegate
• Multicasting is a delegate which holds the reference of more
than one method.

• Delegates are combined and when you call a delegate then a


complete list of methods is called.

• All methods are called in First in First Out(FIFO) order.

• ‘+’ or ‘+=’ Operator is used to add the methods to delegates.

• ‘–’ or ‘-=’ Operator is used to remove the methods from the


delegates list.
• Note: Remember, multicasting of delegate
should have a return type of Void otherwise it
will throw a runtime exception.

• Also, the multicasting of delegate will return the


value only from the last method added in the
multicast.

• Although, the other methods will be executed


successfully.
Example
using System;
public delegate void delmethod(int x, int y);
class Program
{
public void plus_Method1(int x, int y)
{
Console.WriteLine("You are in plus_Method");
Console.WriteLine(x + y);
}
public void subtract_Method2(int x, int y)
{
Console.WriteLine("You are in subtract_Method");
Console.WriteLine(x - y);
}
static void Main(string[] args)
{
Program obj = new Program();
delmethod del = new delmethod(obj.plus_Method1);

// Here we have multicast


del += new delmethod(obj.subtract_Method2);
// plus_Method1 and subtract_Method2 are called
del(50, 10);
Console.WriteLine();
//Here again we have multicast
del -= new delmethod(obj.plus_Method1);
//Only subtract_Method2 is called
del(20, 10);
}
}
Events with Delegates
What is Events ??
• Events are user actions such as key press, clicks, mouse
movements, etc., or some occurrence such as system generated
notifications.

• An Event is an automatic notification to users that some action has


occurred.

• Applications need to respond to events when they occur.

• Events are used for inter-process communication.

• Event Handlers can’t return a value. They are always void.


Using Events with Events Delegates
• An event is nothing but an encapsulated delegate.

• As we have learned in the previous section, a delegate is a


reference type data type.

• to declare an event, use the event keyword before declaring a


variable of delegate type, as below:

• Example: Event Declaration


– public delegate void someEvent();
– public event someEvent someEvent;
Example

using System;
public delegate void DelEventHandler();
class Program
{
public static event DelEventHandler add;
static void Main(string[] args)
{
add += new DelEventHandler(USA);
add += new DelEventHandler(India);
add += new DelEventHandler(England);
add();
}
static void USA()
{ Console.WriteLine("Hello USA"); }
static void India()
{ Console.WriteLine("Hello India"); }
static void England()
{ Console.WriteLine("Hello England"); }
}
Example 2
using System; class Demo1
public delegate void Hello(); {
class Demo static void Handler()
{ {
public event Hello e1; Console.WriteLine("Helllo");
public void OnEvent() }
{ public static void Main()
if(e1!=null) {
e1(); Demo d1=new Demo();
else d1.e1+=Handler;
Console.WriteLine("Not Register...."); d1.OnEvent();
} }
} }

You might also like