Delegate is an object that can refer to
a method.
When
we are creating delegates, we are creating an object that can hold a reference
to a method; it necessarily means that a delegate can invoke the method to
which it refers.
As
the delegate refers to a method, the same delegates can be used to call
multiple methods just by changing the method name at the run time; provided the
method (instance or static) match the signature and return type.
Confused?
not a problem, just go through below code snippet of code behind (DelegatesPage.aspx.cs)
of my DelegatesPage.aspx page.
public partial class DelegatesPage : System.Web.UI.Page
{
delegate int Add(int x);
protected void Page_Load(object sender, EventArgs e)
{
Add a = Sum;
Response.Write(a(6).ToString());
Response.Write("<br />");
a = Minus;
Response.Write(a(6).ToString());
}
int Sum(int a)
{
return a + a;
}
int Minus(int b)
{
return b - b;
}
}
In
the above code snippet, we have declared a delegates named Add that accepts
one parameter of integer type. In the Page_Load event, we are assigned Sum method (we will
define & implement this method later on) to the delegates object
"a". That means that now a can be used to invoke the Sum method. In the very
next line, we used "a" that is nothing but the object of delegate Add
to call the Sum method. As the Sum method accepts one parameter so we are
calling "a" with one parameter "6".
As
the Sum method simply add the values are return so calling "a(6)"
will return 12.
As
I said earlier, the delegate simply refers a method so the same delegate can be
used to refer another method and next I have referred Minus method (defined and
implemented later on) and again we are calling Minus method just by using the
delegate object that gives 0 result.
What is Multicast delegates?
As the name suggest (Multicast), it means multiple so A delegate that refers multiple methods is called Multicast delegates. Calling the object of the delegates automatically call all method referred to this delegate. These methods can be attached and detached using "+=" and "-=" to the instance of the delegate.
In
case a delegate returns a value (as in the case of above delegate), the value
returned by last method becomes the return value of the entire delegate
invocation methods.
public partial class DelegatesPage : System.Web.UI.Page
{
// multi-cast delegates
delegate void Add(int x);
protected void Page_Load(object sender, EventArgs e)
{
Add a = Sum;
a += Minus;
a(6);
}
void Sum(int a)
{
Response.Write(a + a);
Response.Write("<br />");
}
void Minus(int b)
{
Response.Write(b - b);
}
}
In the above code snippet, as you can see that we have a Delegate named "Add" that accepts two parameter but does not return any value as it is void.
In
the Page_Load event, we have created the object of the delegate and assigned
Sum method; in the very next line we have attached one more method called Minus
to the same delegates using "+=" operator.
Now
calling the delegates object, simply executes both Sum and Minus method and
gives respective results. So we will get 12 and 0 result. If you have
consumed WCF services, you must have come across multicast delegates scenario.
When to use Delegates?
All
this looks fine, now the important question is why and when to use delegates?
As per MSDN
Use a delegate when:
- An eventing design pattern is used.
- It is desirable to encapsulate a static method.
- The caller has no need access other properties, methods, or interfaces on the object implementing the method.
- Easy composition is desired.
- A class may need more than one implementation of the method.
Sounds complicated? In simple term
Delegates are basically used in
- Abstracting and encapsulating methods
- Callback mechanism
- Asynchronous programming
- Sequential programming etc.