Shortly: event is a way to signal something occurred. In ASP.NET this is tied to signal for example when user clicks a button, or makes a selection in DropDownList. Delegate is a function pointer, which is used as "glue"
to tie a method to a this event so that method will be called when the event occurs. Basically, when Button is clicked, a corresponding method will be called in response to that, for example a Button_Click method on your page.
I think you can find out more info in books or MSDN. But to make a quick start for you:
Delegates are the classes used to wrap callback function into managed shape. Internally delegate instance holds reference to function or functions chain that are match signature declared by the delegate.
E.g. lets define delegate (I'm using c#):
public delegate void FooCallback(string aMessage);
So, somewhere in code you can create instance of FooCallback delegate to point a particular function:
FooCallback foo = new FooCallback(Foo);
public void Foo(string aMessage)
{
Console.WriteLine(aMessage); }
Notice that Foo void should be the same signature as delegate was declared, otherwise you fail to create delegate.
Now, by having delegate created, we start to call callback function specified in constructor:
foo ("Hi there!");
You can call delegate function asynchronously:
foo.BeginInvoke ("Hi there!");
To add one more callback function in a chain you could:
foo += new FooCallback(Foo2);
public void Foo2(string aMessage)
{
Console.WriteLine(aMessage); }
So that, now when you call foo ("Hi there!") it will call two functiona Foo and Foo2, and so on.
Event is some kind of wrapper to encapsulate the delegate. It's like you have a field and you wraps access to it using the property (OOP encapsulation).
I fact, you could do without events. You can declare public field of some kind of delegate and use it to register callback function and call them. But this is not safe. As everybody have access to your delegate object and can remove all registered callback
functions by simply setting the value, like the obj.Click = new EventHandler(Click) - this will discard all previously registered handlers.
By having event member outer code can't harm you. As the only it can do is to add or remove handler, so that this code won't even compile obj.Click = new EventHandler(Click), if obj.Click is an event member, all you can is obj.Click += new EventHandler(Click)
or obj.Click -= new EventHandler(Click)
Got it? [;)]
Never ask users what they want, or they'll tell you
tusharposhiy...
Member
14 Points
63 Posts
What is delegates in asp.net
Jun 29, 2007 04:50 PM|LINK
hello....
What is Delegates?
What is Events?
plz reply with give me e.g.
Thx,
Tushar
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: What is delegates in asp.net
Jun 29, 2007 05:14 PM|LINK
Hi,
it is a long topic and you'll find long explanation of it at http://msdn2.microsoft.com/en-us/library/17sde2xt.aspx
Shortly: event is a way to signal something occurred. In ASP.NET this is tied to signal for example when user clicks a button, or makes a selection in DropDownList. Delegate is a function pointer, which is used as "glue" to tie a method to a this event so that method will be called when the event occurs. Basically, when Button is clicked, a corresponding method will be called in response to that, for example a Button_Click method on your page.
Teemu Keiski
Finland, EU
uncleb
Star
9644 Points
1864 Posts
Re: What is delegates in asp.net
Jun 29, 2007 05:27 PM|LINK
here is a link for delegates
http://msdn.microsoft.com/msdnmag/issues/03/02/Delegates/default.aspx
Events are the basic building block of most object-oriented languages.
When you create an object, you have certain events that can be triggered.
the easiest example is a button. When you "click" the button with your mouse, an event occurs that you can "capture" and write code for.
For the button, the event is OnClick. What ever you want to happen, you put in the sub Button1_Click()
Different controls have different events
All that wander, are not lost...
What were we talkin bout
ArtemL
Participant
1258 Points
251 Posts
Re: What is delegates in asp.net
Jun 29, 2007 05:33 PM|LINK
I think you can find out more info in books or MSDN. But to make a quick start for you:
Delegates are the classes used to wrap callback function into managed shape. Internally delegate instance holds reference to function or functions chain that are match signature declared by the delegate.
E.g. lets define delegate (I'm using c#):
So, somewhere in code you can create instance of FooCallback delegate to point a particular function:
Notice that Foo void should be the same signature as delegate was declared, otherwise you fail to create delegate.
Now, by having delegate created, we start to call callback function specified in constructor:
foo ("Hi there!");You can call delegate function asynchronously:
foo.BeginInvoke ("Hi there!");To add one more callback function in a chain you could:
So that, now when you call foo ("Hi there!") it will call two functiona Foo and Foo2, and so on.
Event is some kind of wrapper to encapsulate the delegate. It's like you have a field and you wraps access to it using the property (OOP encapsulation).
I fact, you could do without events. You can declare public field of some kind of delegate and use it to register callback function and call them. But this is not safe. As everybody have access to your delegate object and can remove all registered callback functions by simply setting the value, like the obj.Click = new EventHandler(Click) - this will discard all previously registered handlers.
By having event member outer code can't harm you. As the only it can do is to add or remove handler, so that this code won't even compile obj.Click = new EventHandler(Click), if obj.Click is an event member, all you can is obj.Click += new EventHandler(Click) or obj.Click -= new EventHandler(Click)
Got it? [;)]
tusharposhiy...
Member
14 Points
63 Posts
Re: What is delegates in asp.net
Jun 30, 2007 07:58 PM|LINK
hello joteke
thx to reply me
but i m satisfied more link and more details plz send.
thx,
Tushar
junjunjun
Member
2 Points
4 Posts
Re: What is delegates in asp.net
Jul 01, 2010 07:29 AM|LINK
thank you
can you help me please?
my problem is here...
http://forums.asp.net/t/1574270.aspx
thank you.