Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Mar 2, 2011, 8:29 PM EST
Hi,
Basically it is similar like the old "C" - function pointer, where functions can be assigned like a variable and called in the run time based on dynamic conditions. C# delegate is the smarter version of function pointer which helps software architects a lot, specially while utilizing design patterns.
At first, a delegate is defined with a specific signature (return type, parameter type and order etc). To invoke a delegate object, one or more methods are required with the EXACT same signature. A delegate object is first created similar like a class object created. The delegate object will basically hold a reference of a function. The function will then can be called via the delegate object.
Refer this article. http://www.c-sharpcorner.com/UploadFile/subhendude/4621/
VulpesRank #55 · 29.2K reputationPosted Mar 3, 2011, 1:31 AM EST
1. Delegates enable you to write code which is more generic. For example, if you declare a method parameter to be of a particular delegate type, then you can pass ANY compatible method as an argument to the method so that it can be called within the body of that method.
2. Delegates are multi-cast which means that you can call not just one, but several, compatible methods with just a single delegate call. The methods to be called are stored in the delegate's 'invocation list'.
Also, when calling unmanaged functions from managed code, delegates provide a bridge between C function pointers and .NET methods which can be used to enable the unmanaged function to call back the latter.
However, the main reason for including delegates in .NET is because they are the basis of events. Events are just a special type of delegate which only allow outside code (i.e. code outside the class in which the event is declared) to add methods to, or remove them from, the event's invocation list. In C# this is done using the special operators += and -= respectively.
Events are more secure than ordinary delegates because, apart from these operations, outside code is unable to examine or manipulate the event's invocation list.
Suthish NairRank #92 · 22.8K reputationPosted Mar 3, 2011, 8:31 AM EST
A delegate is a reference to a method, the caller has no need access other properties,
methods, or interfaces on the object implementing the method.
Purpose of Delegates in C#