KARPACH

WEB DEVELOPER BLOG

What is delegates and how do we use them in asp.net?

There are different types in C#: int, bool, float etc. Also, you can declare custom types using keyword class or struct. The types above represent nouns or objects. Delegates are custom types that represent verbs or methods. Delegate is a C# keyword similar to class or struct.

The delegate is used to declare a reference type that can be used to encapsulate a named or anonymous method. Delegates are similar to function pointers in C++; however, delegates are type-safe and secure. In asp.net we are using delegates to create custom events for custom controls.

For example, custom pager control most likely needs to have PageChanged event.

We can declare it like this:

public delegate void PageChangedHandler(object sender, EventArgs e);
public event PageChangedHandler PageChanged;

Then whenever we need to fire an event:

if (PageChanged != null) // Checks if user assigned any event handler
{
    PageChanged(this, new EventArgs());
}  

And then we can use our custom pager control as follow:

<cc:PostsPager ID="PostsPager" runat="server" OnPageChanged="PostsPager_PageChanged" />
Posted on July 9, 2008 by

Comments

Posted on 10/7/2008 04:45:27 AM by jitender

Nice

Posted on 9/1/2009 11:41:36 PM by piyush

It is good, example , but there should be more explaination.

Posted on 4/16/2010 12:17:23 AM by rakesh

please explain the delegates with a suitable examle which i can understand
There are simple types such as int, bool, string. Delegate is a type for a function method. By declaring delegate you are declaring signature of method stub. In ASP.NET it mostly useful for events, example of such usage I have below.

Posted on 5/11/2010 03:09:39 AM by swati

it should be preety clear

Posted on 8/2/2010 03:06:21 AM by jeetendra patro

This is wrote by simple language it has understand for student vary easy.

Posted on 9/9/2010 02:49:00 AM by om kumar

i m not satisfied with delegate. there r not define clear

Posted on 11/9/2010 08:16:21 PM by pramod kumar

How to call delegate through his address in asp.net

Posted on 1/24/2011 10:41:16 PM by Ajinkya

Should be more stp by step example. Means what to do exactly. Because beginners cannot understand easily.

Posted on 2/6/2011 10:46:43 PM by radhika

Its not more useful.please elaborate more

Posted on 3/7/2011 02:18:53 AM by Ranjan Srivastava

Not enough to understand.

Posted on 3/23/2011 05:54:36 AM by abinaya

its not understandable for the beginers.so pls give basic idea of delegates in asp..net

Posted on 3/25/2011 12:27:10 AM by edvin thunderbold

lack of explanation

sorry u better improve to explain better

Posted on 7/4/2011 07:39:22 AM by suresh babu

sorry its little bit hard to understand try to explain short and step-by-step process

Posted on 8/5/2011 02:30:58 AM by sajjan

will u please give some better example in terms of webpage and its controls…..

Posted on 9/16/2011 03:34:38 AM by Ajay Pal

plz provide notes what is delegate in simple words

Posted on 11/6/2011 09:59:27 PM by mahavir

i am confusing about delegate . what is advantage of using delegates
There is no advantages. Delegates are just another tool.

Posted on 1/9/2012 06:19:32 PM by senthil

nice explanation …

Posted on 1/26/2012 07:17:40 PM by kiran

1.deltgates are refer to a methode or functions.

2.we can consume a deloigate by using deligate object.

3.these can be used to define call back methode.

4.by using deligate we can call multiple methods at the same time.

ex:

public void add(int x,int y)  
{
}

i need to refer this add function

public deligate void sampledeligate(int x,int y);

creating a deligate

sampledeligate x=new sampledeligate(add);

x(20,10);