This article describes how can we use a delegate in a real time application to build dynamic components. I will write this article in two phases and the first phase goes here.

Introduction

Many time we get questions like where can we use delegate ? Give us a practical example .In fact I saw 2-3
similar questions in this forum so thought of writing this article.

In this age programmers are more interested in building dynamic
and reusable component. There are scenarios where a normal function will not
help to fulfil the purpose. Below are two important features in delegate which
helps to make a component dynamic.

1-1. It allows to inject code by the client(I will describe
in this article)

2-2. Multi-casting (I will describe in the next
article)

Inject functions by client code.

Ok let's do a practical example to make it clear. One
important use of delegate is with the help of delegate we can develop a component which client can
use and inject their functionality to it. In the below example I have shown a
UserRegistration Component. Client will call this component to register a user
,however it entirely depends on the client to implement how they want to notify users
about their registration via email or via SMS .

namespace
RegistrationModule

{

publicdelegatevoidNotifyUser(ContactDetail
objContact);

classRegisterUser{

publicvoid Register(Userinfo
objUser, NotifyUser myFunc){

//code to
save the user details to the database

//

myFunc(objUser.objContact);

}

}

classUserinfo{

publicstring name;publicint age;publicContactDetail objContact;}

classContactDetail{

publicstring email;publicstring mobNo;}

}

Client Code:

Lets say first client wants to send SMS to notify their
user.So below will be their implementation.

I have given the comment to explain the code.

//Below function is having same signature as delegate NotifyUser and will be attached to the delegate.publicvoid SendSMS(ContactDetail
objContact)

That's it.Now client-1 user will get user registration success message as an SMS and client-2 will get user registration success message as an email. Important thing to notice here is SendSMS and SendEmail is
the code written by the client however it is invoked by the RegistrationModule
what in other words we can say delegates allow us to inject functionality.

Conclusion

I hope this will helps to give a practical use of delegate.In
the next article I will give a practical implementation of multicasting.Please feel free to email me in case you have any question on the same.

Hi Satya,
thanks for an excellent article. but kindly excuse me. i have few doubts in this.
as i am not so good in C# ,the following question may sound you very silly.

1) public void Register(Userinfo objUser, NotifyUser myFunc)
{
//code to save the user details to the database
//
myFunc(objUser.objContact); -- > should not you use new here? if so why? to me normally we create a delegate instance as <delegatename> <variable name> = new <delegatename(methodname)>;
please clarify.

You are right , we initialize the delegate using
<delegatename> <variable name> = new <delegatename(methodname)>
and the same is done in my client code sample,
NotifyUser sendEmail = new NotifyUser(SendEmail);

here
NotifyUser - <delegatename>
SendEmail/SendSMS - methodname

here client is expected to initialize the delegate ,so the delegate is intentionally left un-initialized or we can initialize with a default blank implantation for the delegate.