Delegates:
Delegates are funcation pointers which are used to map the funcations to delegate.
Delegates call the method which ever funcations have identitcal signatures with that declared delegate.
Every Delegate implicitly derived from system.Delegate and system.Multicast Delegate classes.
We can get invoke method implicitly from system.delegate class.
Delegates are 3 types:
SingleCast Delegates , MultiCast Delegates, Event Delegates.
For any Delegate Declaration is must. Then initialization function that declared delegate.
Single Cast Delegates:
Single cast delegate can call single funcation at a time.
Delcare
public delegate void sample();
Initialization
sample s = new sample(Programs.myfuncation);
Calling
S(); or // s.Invoke();
Delegate calling funcation
public static void myfuncation()
{
Console.Write("yes called");
}
Here Simple() is delegate name and myfuncation is funcation name. both have same singature so I can able to map the simple delegate to myfunction is possible.
This simple can map multiple functions also but those function must have same signature with delegate that is called MultiCast Delegate.
Multi Cast Delegates:
I have declared one more function called anotherfuncation() like below
public static void anotherfuncation()
{
Console.Write("another function is called");
}
Anotherfunction signature is same as simple() delegate signature so the same delegate can map this function also. So this is called multicast delegate.
Ex:
1st way sample s = Programs.myfuncation;
s = myfuncation;
s();
s = Programs.anotherfuncation;
s = anotherfuncation;
s();
Or
2nd way
sample s = new sample(Programs.myfuncation);
sample s1 = new sample(Programs.anotherfuncation);
s = s + s1; // Multi Cast.
s.Invoke();
Or
3rd Way
sample s = new sample(Programs.myfuncation); // 1 funcation initialization
// s.Invoke(); ---not required why because single delegate can point out multiple funcation at a time.
s += Programs.anotherfuncation; // 2 funcation intialization
s.Invoke(); // both are invoked.
1st way we can easily say that same delegate 2 times initialized different functions.
2nd way we single delegate points both funcations. using s=s+s1; concatenating.
3rd way we single delegate points both funcations using += operator.
Like another Delegate.
public delegate void sample1(int i);
this delegate can handle only whatever funcation which takes single parameter that is also integer value.
Exdemo:
sample1 s1 = Programs.myfuncation1;
s1.Invoke(48);
Console.Read();
public static void myfuncation1(int value)
{
Console.Write("yes value is passed");
}
As like above simple1() delegate can map the function which ever have same signature like below.
public static void myfuncation2(int value)
{
Console.WriteLine(" ");
Console.Write("yes value is passed 2");
}
Then mapping
sample1 s1 = Programs.myfuncation1;
//s1.Invoke(48);
s1 += Programs.myfuncation1;
s1.Invoke(78);
Console.Read();
Event Delegates:*****
Every Event Delegate is a multicast delegate.
Represents the method that will handle an event that has no event data.
Delegate Declaration.
Event Delclaration.
Mapping the Event to delegate singature as like MultiCast delegate.
Ex:
Delegate Declaratiion
public delegate void EventHandler(object sender, EventArgs e);
EventHandler is name of the delegate.
public void Button1_Click(object sender, EventArgs e){};
Click signature is same as EventHandler signatureinternally delegate points.
control class:
Event Declaration // using Event keyword
public event EventHandler Click; // Event return type is delegate.
Click event return type is EventHandler means click uses the signature of EventHandler. Then points through delegate.
Mapping to the particulat Event: so type safe.
this.button1.Click += new System.EventHandler(this.button1_Click);
Example:
class EventsExample
{
public delegate void MyDelegate();
public event MyDelegate DemoEvent;
static void Main()
{
EventsExample obj = new EventsExample();
obj.DemoEvent += new MyDelegate(EventsExample.fubn1);
Console.Read();
}
static void fubn1()
{
Console.WriteLine("k.ToString()");
}
}
No comments:
Post a Comment