multicast delegate |
MULTICAST DELEGATE
If we have
more than one method with same signature, here signature take as the same
return type and same parameter accepts. Than for those methods we use multicast
delegate to call all that method with one delegate name.
In this
topic we will discuss about three step to perform multicast delegate.
1. Defining the delegate
It is same like as delegate that’s
the delegate signature should match with method
signature except
name. The multi task will perform in creating instance part.
SYNTAX
[<
Modifier >] delegate <return type> Name (parameter list)
2. Instantiating the instance of multicast delegate
The main difference between delegate and
multicast delegate is going to declare here. We will create multiple method reference
by using one delegate name like this.
<Delegate
name> d.name = new <delegate name> (reference method)
This syntax can be return like this
<Delegate
name> d.name = <1st reference method>
d.name
+= <2nd reference method >
Here we use d.name for both methods and when
we give one time values theses method will execute by same value see the
example to clarify.
3. Calling the multicast delegate
It is also same like delegate calling. Calling
will do in two different ways.
1st way: <d.name>
(value1, value2… value n) //as
above mention d.name
2nd way: <d.name>.invoke (value1, value2… value n)
When we call one time all the linked method
will execute using same values.
Points to be noted while going to perform multicast
delegate
There some
points that should keep in mind when decide to make multicast delegate that’s
1. We use multicast delegate in such a time where multi method has
to execute by same values.
2. Before using of this delegate make sure that all your methods
are non-value returning type otherwise only the last method result will be shown
and rest all are overwrites by the last one. If your methods are value
returning type it better to use delegate instead of multicast delegate.
EXAMPLE OF MULTICAST DELEGATE IN C#
example of multicast delegate c# |
Suppose we
have methods about square problem, area of square ( ) and perimeter of square (
) are two diff. method let see how this two method execute using multicast
delegate.
Delegate class
namespace MulticastDeg
{
public delegate void SquareForm(double
Lenght); //defining delegate stage 1
class Square
{
public void AreaOfSquare(double
length)
{
Console.WriteLine("area of
square = " + Math.Pow(length,2));
}
public void PremOfSquare(double
length)
{
Console.WriteLine("perimeter
of square = " + 4*length);
}
}
}
|
Main class
namespace MulticastDeg
{
class Program
{
static void Main(string[] args)
{
Square sq = new Square();
SquareForm sqf = sq.AreaOfSquare; //creating instance stage 2
sqf += sq.PremOfSquare; // multicasting
Console.WriteLine("enter the
lenght of sqaure");
double length = double.Parse(Console.ReadLine());
sqf(length); //calling the
delegate stage 3
Console.ReadKey();
}
}
}
|
0 comments:
Post a Comment
Please do not enter any spam link in the comment box