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. Types above represent nouns or objects. Delegates are custom types that represent verbs or methods. Delegate is 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 an 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 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 Wednesday, July 9, 2008 by | Comments (15) | Add Comment

Comments

Gravatar

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

Nice

Posted on 10/7/2008 4:45:27 AM by jitender #
Gravatar

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

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

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

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

please explain the delegates with a suitable examle which i can understand

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

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.

Gravatar

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

it should be preety clear

Posted on 5/11/2010 3:09:39 AM by swati #
Gravatar

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

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

Posted on 9/9/2010 2:49:00 AM by om kumar #
Gravatar

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

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

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

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

Its not more useful.please elaborate more

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

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

Not enough to understand.

Posted on 3/7/2011 2:18:53 AM by Ranjan Srivastava #
Gravatar

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

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

Posted on 3/23/2011 5:54:36 AM by abinaya #
Gravatar

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

lack of explanation
sorry u better improve to explain better

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

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

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

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

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

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

Posted on 8/5/2011 2:30:58 AM by sajjan #
Gravatar

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

plz provide notes what is delegate in simple words

Posted on 9/16/2011 3:34:38 AM by Ajay Pal #
Gravatar

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

i am confusing about delegate . what is advantage of using delegates

Posted on 11/6/2011 9:59:27 PM by mahavir #
Gravatar

There is no advantages. Delegates are just another tool.

Gravatar

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

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);

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

New Comment

Your Name:
Email (for internal use only):
Comment:
 
Code above:

Categories

Recent Tweets

  • Simon Ince's Blog: Hierarchies with HierarchyID in SQL 2008 http://t.co/xSDwiF6rRS.
  • Visual Studio 2010 WAS painfully slow - CodeProject http://t.co/Usba1x6CZy

Valid HTML5