Delegates
A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.
The signature of a single cast delegate is shown below:
delegate result-type identifier ([parameters]);
where:
- result-type: The result type, which matches the return type of the function.
- identifier: The delegate name.
- parameters: The Parameters that the function takes.
Delegates allow a clearer separation of specification and implementation. A delegate specifies the signature of a method, and authors can write methods that are compatible with the delegate specification.
Multicast delegates
With multicast delegate, it is just like a normal delegate but it can point to multiple functions so on invocation of such delegate it will invoke all the methods and functions associated with the same one after another sequentially.
Events:
Events are wrapper around the delegate. It sits on top of the delegate and provides only the necessary encapsulation so that the destination objects can subscribe to the Event and not have full control on the delegate object.
Events are declared using delegates. The delegate object encapsulates a method so that it can be called anonymously. An event is a way for a class to allow clients to give it delegates to methods that should be called when the event occurs. When the event occurs, the delegate(s) given to it by its clients are invoked.
We will consider an example implementation of Logger application, which can be used to log messages in Console, File or Database etc.
We will declare a delegate named LogWriteDelegate which accepts one parameter named message.
public delegate void LogWriteDelegate(string message);
Next we will declare an Event of type LogWriteDelegate
public event LogWriteDelegate LogWriteEvent;
In our sample Logger the FileLogger and ConsoleLogger will subscribe or register to the above mentioned event so that when the Write method of logger is called the respective logic within the Write method of FileLogger and ConsolLogger will also gets invoked.
using (var logger = new Logger("testing", EntryType.Debug))
{
logger.LogWriteEvent += new Logger.LogWriteDelegate(consoleLogger.Write);
logger.LogWriteEvent += new Logger.LogWriteDelegate(fileLogger.Write);
logger.Write();
}
You can notice here a multicast delegate; the LogWriteEvent is being registered with Console and File log write methods.
While firing an Event within the Logger object, it is always a good practice to check whether the event is registered or not.
public void Write()
{
if (LogWriteEvent != null)
LogWriteEvent(ToString());
}
Here the LogWriteEvent holds the address of the methods console.Write and fileLogger.Write. When firing an event it will sequentially call the respective methods which are pre-registered to this Event.
Note: The Logger application is designed in such a way that in future you can have multiple implementations of Loggers. Right now we have Console and File Logger; if you are interested in implementing a Database logger we can create a new Class named DatabaseLogger which will implement ILogger interface. Provide necessary Write method logic so that it will log messages to database.
Ones you have done with the DatabaseLogger implementation, you just have to create an instance and register the event as we have done for Console and File Loggers.
Hi,
When we make an object of a class then we can access all function of a class and Base class functions too. Here we can see that a user can simple make an object of a
class and access all information of class. But in real word this is drawback of an object.
This is a nice article so thanks for sharing your knowledge. There are few other links that also described “Delegate in C#” in a good way.
http://mindstick.com/Articles/2ace6310-943e-4f1b-a193-c1b5f61acd54/default.aspx?Delegate+in+C+Net
http://www.dotnetperls.com/delegate
http://www.codeproject.com/Articles/11657/Understanding-Delegates-in-C