Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is the main purpose of delegates in .net?we dont have another approch in place of delegates.what its job?

thanx for ur answers
Posted

You need to read a book Sir! Really, there's no point spouting on about it here, there's so much on the net already.

You could start here:

http://msdn.microsoft.com/en-us/magazine/cc301810.aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Nov-11 8:00am    
I was afraid even to open this page :-) Right, there is no point, my 5.
--SA
Sergey Alexandrovich Kryukov 16-Nov-11 8:12am    
Even though I voted 5, I want to add a note: this article does not explain important things.

I actually did not see a single Microsoft article which tells all the truth about delegates. Please see my comment to the solution (pretty much incorrect) by Rais. I put some interesting detail in my article: "Dynamic Method Dispatcher", http://www.codeproject.com/KB/dotnet/DynamicMethodDispatcher.aspx. Please see -- there are some facts which are easy to check up but most developers did not suspect about.
--SA
Rob Philpott 16-Nov-11 8:27am    
Didn't read it to be honest, but the title sounded like a starting point. When I started with .NET the first book I read was.. can't remember, but the second edition of it was called CLR via C# by Jeffrey Richter. I think its now in its third revision. Should be compulsary reading. Also, C# in a nutshell by Josepth Albahari is an awesome read and deadly concise. If anybody wants to learn about things like delegates you couldn't do better than reading those two. Will check your link out too. :)
Delegate is a Pointer to a Function/Method, which holds the reference of the Function/Method. (I know its too theoretical :))

Information of Delegates on MSDN.
http://msdn.microsoft.com/en-us/library/ms173171.aspx

CodeProject Article on Delegates.
Events and Delegates simplified
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Nov-11 8:07am    
(Sight) Rais, this is not true. If here is some truth in it, but only in zero approximation. Do you know that an instance of a delegate is a class (surprise!)? It provides access to a container called invocation list. That is, each delegate instance is potentially multicast. And each single handler can be a pointer only for static members. For non-static members, it's at least two pointers: one points to code, another one references "this". But in fact it's even more complex.
Pretty far from you explanation, isn't it? How about your understanding?
--SA
Sergey Alexandrovich Kryukov 16-Nov-11 8:13am    
I put some interesting detail in my article: "Dynamic Method Dispatcher", http://www.codeproject.com/KB/dotnet/DynamicMethodDispatcher.aspx. Please see -- there are some facts which are easy to check up but most developers did not suspect about.
--SA
RaisKazi 16-Nov-11 8:26am    
Sure, will go through it.
RaisKazi 16-Nov-11 8:23am    
It's a wide topic altogether. Agree, its a complex concept(which is becoming more complex day by day :)), Microsoft has introduced "Predicate Delegates" from .Net Framework 2.0. I think, that CodeProject Article may help OP to begin with.(Ideally Google/MSDN would have revealed all, if OP would know how to search on Google/MSDN.)
Sergey Alexandrovich Kryukov 16-Nov-11 15:00pm    
This is a very advanced matter (but I don't say "complex", just advanced; main problem is lack of detailed documentation) which is not, strictly speaking, required to be known in detail just for using delegates, but the main ideas must be understood.
--SA
The main purpose is to hold information on how to call a method (or group of methods) with a given signature (parameter types and result type).
Alternatives? Sure. You can always create an interface with a Run method.
But delegates are still faster than interfaces.

They are largely used on Events, but you can use them every time you want to receive a "function" as a parameter. One of the overloads of List.Sort method receives a delegate, so you can do your on comparison.
 
Share this answer
 
Delegates have the following properties:

Delegates are similar to C++ function pointers, but are type safe.

Delegates allow methods to be passed as parameters.

Delegates can be used to define callback methods.

Delegates can be chained together; for example, multiple methods can be called on a single event.

Methods don't need to match the delegate signature exactly. For more information, see Covariance and Contravariance

C# version 2.0 introduces the concept of Anonymous Methods, which permit code blocks to be passed as parameters in place of a separately defined method.
 
Share this answer
 
Delegate will be useful whenever r going to use USER CONTROL in the aspx page.This the real time usage.
You can search in this way and you can find more code with examples.
 
Share this answer
 
Comments
Rob Philpott 16-Nov-11 7:55am    
Hmm. Delegates have nothing to do with USER CONTROLS, aspx or real time usage, so I don't think that's helpful to be honest.
[no name] 16-Nov-11 7:56am    
Delegates have no connection with user controls. Perhaps you need to do research also.
[no name] 16-Nov-11 7:58am    
dont provide wrong answers please
Sergey Alexandrovich Kryukov 16-Nov-11 8:03am    
Agree with you and others. This is easier to claim than to meet, but at least it's good to avoid answering in the fields which a person is not comfortable with. There are delusions though...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900