Delegates
A delegate in C# is similar to a function
pointer in C or 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. Unlike function pointers in C or C++,
delegates are object-oriented, type-safe, and secure.
A delegate is defined with a specific
signature (return type, parameter type and order etc). To invoke a delegate
object, one or more methods are required with the EXACT same signature. A
delegate object is first created similar like a class object created. The
delegate object will basically hold a reference of a function which has the same
signature.
Syntax for Declaring a Delegate:
Public delegate
<return type> <Delegate Name> (parameters);
Example: public delegate int Calculate (int x, int y);
We can use the delegates with out
parameters also.
Delegates are of 2 types
1)
Single Cast Delegate
2)
Multi Cast Delegate
Single Cast Delegate: It is a delegate which holds the reference of one method.
Example program for Single cast
delegate
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BloggerExamples
{
//delegate
declaration with two parameters integers.
public delegate int MyDelegate(int x, int y);
class SingleDelegateExample
{
public static int Add(int x, int y)
{
return x + y;
}
static void Main (string[] args)
{
MyDelegate del1 = new MyDelegate(Add);
int addResult = del1(5, 5);
Console.WriteLine("5 + 5 = {0}\n", addResult);
Console.ReadLine();
}
}
Multi Cast Delegate: It
is a delegate which holds the reference of more than one method.
Delegate's
multicast means that a delegate object can maintain a list of methods to call,
rather than a single method.
If you want to add a method to the invocation list of a delegate object, you simply make use of the overloaded += operator, and if you want to remove a method from the invocation list you make use of the overloaded operator -=.
In Multi-casting you
create a single delegate that will invoke multiple encapsulated methods. The
return type of all the delegates should be same.
Note: The question why are we using Multi-cast
delegates when Single-cast delegates are enough?
Answer to this
question is that if we want to call three methods when a button is clicked. In
this situation we use the Multi-cast delegates instead of single cast delegate
why because single cast can hold the reference of only one method. But Multi
cast delegate can hold references of multiple methods
The Multi-cast
delegates are used with events where multiple calls to different methods are
required.
The following program
will demonstrate the Multi cast Delegate
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Blogger Examples
{
//delegate
declaration with two parameters integers
public delegate void MulticastDelegate(int x, int y);
class MyMulticastDelegate
{
Public static void Add (int x, int y)
{
Console.WriteLine ("You are in Add ()
Method");
Console.WriteLine ("{0} + {1} =
{2}\n", x, y, x + y);
}
public static void Multiply (int x, int y)
{
Console.WriteLine ("You are in multiply ()
Method");
Console.WriteLine("{0} X {1} = {2}", x, y, x * y);
}
static void Main (string[] args)
{
MulticastDelegate del = new MulticastDelegate (Add);
Console.WriteLine ("****calling Add() and Multiply()
Methods.****\n\n");
Console.WriteLine("\n\n****Add() Method
removed.****\n\n");
Console.ReadLine();
}
}
}
OutPut: