•
Delegate
in C# allows to dynamically changing the reference to the methods in a class.
•
A
delegate is a reference type variable, which holds the reference to a method.
This reference can be changed at runtime.
•
Primarily
used for implementing events and the call-back methods.
Instantianing
Delegate
public void DelegateFunction(string ArgValue)
{
//Method
implementation.
}
Declaring
Delegate
Delegate return-type delegate-name (parameter-list);
C#
provides support for Delegates through the class called Delegate in the System namespace.
Delegates
are of two types.
-
Single-cast delegates
-
Multi-cast delegates
A
Single-cast delegate is one that can refer to a single method whereas a
Multi-cast delegate can refer to and eventually fire off multiple methods that
have the same signature.
The
signature of a delegate type comprises are the following.
The name
of the delegate
The
arguments that the delegate would accept as parameters
The
return type of the delegate
class clsDelegate
{
public delegate int simpleDelegate (int a, int b);
public int addNumber(int a, int b)
{
return (a+b);
}
public int mulNumber(int a, int b)
{
return (a*b);
}
static void Main(string[] args)
{
clsDelegate clsDlg = new clsDelegate();
simpleDelegate addDelegate = new simpleDelegate(clsDlg.addNumber);
simpleDelegate mulDelegate = new simpleDelegate(clsDlg.mulNumber);
int addAns = addDelegate(10,12);
int mulAns = mulDelegate(10,10);
Console.WriteLine("Result by calling the addNum method using a delegate: {0}",addAns);
Console.WriteLine("Result by calling the mulNum method using a delegate: {0}",mulAns);
Console.Read();
}
}
{
public delegate int simpleDelegate (int a, int b);
public int addNumber(int a, int b)
{
return (a+b);
}
public int mulNumber(int a, int b)
{
return (a*b);
}
static void Main(string[] args)
{
clsDelegate clsDlg = new clsDelegate();
simpleDelegate addDelegate = new simpleDelegate(clsDlg.addNumber);
simpleDelegate mulDelegate = new simpleDelegate(clsDlg.mulNumber);
int addAns = addDelegate(10,12);
int mulAns = mulDelegate(10,10);
Console.WriteLine("Result by calling the addNum method using a delegate: {0}",addAns);
Console.WriteLine("Result by calling the mulNum method using a delegate: {0}",mulAns);
Console.Read();
}
}
Implementing
a multi-cast delegate
public delegate void TestDelegate();
class Test
{
public
static void Display1()
{
Console.WriteLine("This
is the first method");
}
public
static void Display2()
{
Console.WriteLine("This
is the second method");
}
static
void Main()
{
TestDelegate
t1 = new TestDelegate(Display1);
TestDelegate
t2 = new TestDelegate(Display2);
t1
= t1 + t2; // Make t1 a multi-cast delegate
t1();
//Invoke delegate
Console.ReadLine();
}
}
Output:
This is the first method
This is the second method
Events
•
An
event is an action or occurrence, such as clicks, key presses, mouse movements,
or system generated notifications.
•
An
application can respond to events when they occur.
•
The
.net Framework event model uses delegates to bind event notifications with
methods known as event handlers.
•
The
events are declared and raised in a class and associated with the event
handlers using delegate with in the same class or other classes.
•
Events
use the publisher and subscriber model.
•
A
publisher is an object that contains the definition of the event and the
delegate.
•
A
subscriber is an object that wants to accept the event and provide a handler to
the event.
public class Metronome
{
public event TickHandler Tick;
public EventArgs e = null;
public delegate void
TickHandler(Metronome m, EventArgs e);
public void Start()
{
while (true)
{
System.Threading.Thread.Sleep(3000);
if (Tick != null)
{
Tick(this, e);
}
}
}
}
public class Listener
{
public void Subscribe(Metronome
m)
{
m.Tick += new
Metronome.TickHandler(HeardIt);
}
private void HeardIt(Metronome
m, EventArgs e)
{
System.Console.WriteLine("HEARD
IT");
}
}
class Test
{
static void Main()
{
Metronome m = new Metronome();
Listener l = new Listener();
l.Subscribe(m);
m.Start();
}
}
Ex:With
EventHandeler
//declaring the event handler delegate
delegate void ButtonEventHandler(object source,int
clickCount);
class Button
{
//declaring the event
public event ButtonEventHandler ButtonClick;
//Function to trigger the event
public void clicked(int count)
{
Console.WriteLine("\nInside Clicked !!!");
//Invoking all the event handlers
if (ButtonClick != null) ButtonClick(this,count);
}
}public class Dialog
{ public
Dialog()
{
Button b = new Button();
//Adding an event handler
b. ButtonClick += new ButtonEventHandler(onButtonAction);
//Triggering the event
b.clicked(1);
b.ButtonClick += new
ButtonEventHandler(onButtonAction);
b.clicked(1);
//Removing an event handler
b.ButtonClick -= new
ButtonEventHandler(onButtonAction);
b.clicked(1);
b.ButtonClick -= new ButtonEventHandler(onButtonAction);
b.clicked(1);
}
static void Main()
{
new Dialog();
}
//Event Handler function
public void onButtonAction(object source,int
clickCount)
{
Console.WriteLine("Inside Event Handler
!!!");
}
}
Output:
Inside Clicked !!!
Inside Event Handler !!!
Inside Clicked !!!
Inside Event
Handler !!!
Inside Event Handler !!!
Inside Clicked !!!
Inside Event Handler !!!
Inside
Clicked !!!
_____________________________________________________________________
Reach us At: - 0120-4029000; 0120-4029024; 0120-4029025,
0120-4029027; 0120-4029029
Mbl: 9953584548
No comments:
Post a Comment