This
Article will give you brief description about delegate and its types; a delegate in C# is similar to a function pointer
in C or C++. Unlike function pointers in C or C++, delegates are
object-oriented, type-safe, and secure.
There are three steps in defining and using
delegates:
1. Declaration
2. Instantiation
and
3. Invocation
Delegate declaration:
Declaration is
done with keyword delegate, followed by return type, delegate name and
parameters (optional).
public delegate void mydelegate(int x, int y);
Delegate Instantiation:
It’s nothing but creating object for delegate and assigning
function to it. There are different ways to instantiate delegate
mydelegate del = new mydelegate(Add);
Or
mydelegate del = Add;
Note:
The
function should have same return type (above one is void), with same parameter
type and numbers, otherwise compiler will through error.
Delegate Invocation:
Calling
methods associate with the delegate is delegate invocation; it can be
synchronous or asynchronous.
1. Synchronous invocation:
It waits
for the method to complete its execution, and then goes to next statement, why
because it’s maintained in single thread.
del(5,10);
Or
del.Invoke(10, 10);
2. Asynchronous invocation:
It won’t
wait for the associated method to complete its execution; the method execution
is maintained in separate thread, once the process complete it will return to
Targetmethod.
del.BeginInvoke(5,10,new
AsyncCallback(Targetmethod),null)
The
target method should have IAsyncResult
as parameter
Based on method(s)
associate with delegate, it’s classified into two types
1. Singlecast
delegate
2. Multicast
delegate
1. Single cast Delegate:
Singlecast delegates provide functionality to
execute only one method with or without return type.
Example:
public delegate string SingleCastDelegate(string first,string
second);
The function associated with single cast delegate is,
public string
AddString(string firstName, string secondName)
{
return string.Concat(firstName,
secondName);
}
2. Multicast Delegate:
Delegate
who refers to more than one function is multicast delegate, unlike single cast delegate;
there are some restrictions in multicast delegate.
Restriction:
1. Return type of multicast delegate is always
void, because it involves more than one function.
2. It’s
always called in the order, which is assigned to it, for example its assigned
in order like a(), b(), c() means it’s called in same order.
Example:
I’m having four function Add, Difference, Product, Division, now I’m going to add
all this four functions to my delegate, make sure that it follows same
signature as delegate
//Multicast delegate
public delegate void MultiCastDelegateCalculator(int x, int y);
//Adding functions to delegate
MultiCastDelegateCalculator delMulti =new MultiCastDelegateCalculator(calc.Add);
delMulti += calc.Difference;
delMulti
+= calc.Product;
delMulti += calc.Division;
delMulti(5, 10);
And finally invoke the delegate with some input.
We can
also remove function from delegate if it’s not needed, to remove function from
delegate replace + with -.
delMulti -= calc.Product;
delMulti(5, 10);
Now the
function Product will no longer available in delegate.
Attachment:
I enclose
sample project for the understanding of single cast and multicast delegate.
No comments:
Post a Comment