Working with Delegates in C#
page 2 of 5
by Joydip Kanjilal
Average Rating: 
Views (Total / Last 10 Days): 29777/ 90

What are delegates and why are they required?

Delegates are function pointers in C# that are managed and type safe and can refer to one or more methods that have identical signatures. Delegates in C# are reference types. They are type safe, managed function pointers in C# that can be used to invoke a method that the delegate refers to. The signature of the delegate should be the same as the signature of the method to which it refers. According to MSDN, "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. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure."

C# provides support for Delegates through the class called Delegate in the System namespace. Delegates are of two types.

·         Single-cast delegates

·         Multi-cast delegates

A Single-cast delegate is one that can refer to a single method whereas a Multi-cast delegate can refer to and eventually fire off multiple methods that have the same signature.

The signature of a delegate type comprises are the following.

·         The name of the delegate

·         The arguments that the delegate would accept as parameters

·         The return type of the delegate

A delegate is either public or internal if no specifier is included in its signature. Further, you should instantiate a delegate prior to using the same.

The following is an example of how a delegate is declared.

Listing 1: Declaring a delegate

public delegate void TestDelegate(string message);

The return type of the delegate shown in the above example is "void" and it accepts a string argument. Note that the keyword "delegate" identifies the above declaration as a delegate to a method. This delegate can refer to and eventually invoke a method that can accept a string argument and has a return type of void, i.e., it does not return any value. You can use a delegate to make it refer to and invoke a method that has identical signature as the delegate only. Even if you are using multi-cast delegates, remember that you can use your delegate to refer to and then fire off multiple methods that have identical signatures only.

A delegate should always be instantiated before it is used. The following statement in Listing 2 shows how you can instantiate a delegate.



Listing 2: Instantiating a delegate

TestDelegate t = new TestDelegate(Display);

View Entire Article

User Comments

No comments posted yet.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2021  |  Page Processed at 2021-12-02 7:53:10 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search