Click here to Skip to main content
15,846,976 members
Articles / Programming Languages / C#

Delegate: What is this?

Rate me:
Please Sign up or sign in to vote.
2.97/5 (43 votes)
17 Aug 20032 min read 104.8K   1.3K   18   12
This article contains explanation of delegate concept.

Why this article was written

At first, I want to make excuses for my knotty English. However, I shall do everything possible to make this article understandable.

At the second, I want to explain why I wrote this article. Some time ago, I had a discussion with a guy. This guy was leaning programming with C#. He asked me to explain about delegates and events. Actually, I was very surprised. There are a lot of good C# books, useful links, source code samples, and etc. However, during our discussion, I understood that this guy read books about C# programming, browsed suitable web resources, investigated good code samples, but still had questions about delegates!

The current code sample is the result of our discussion. This sample is very simple, it is possible to say, stupid. But, it helped my opponent to understand the essentials of delegates. I hope that this sample and comments will be useful for other people who are learning C# programming.

The article itself

I recommend downloading the demo project before reading the article. This is a C# console application. Unzip the downloaded file and click SLN file to launch the project in .NET Studio.

C# delegate is a callback function. In other words, delegate is a way to provide feedback from class-server to class-client.

C# delegate is smarter then “standard” callback because it allows defining a strict list of parameters which are passed from class-server to class-client.

In our sample, class-server has the name DataHolder. It holds a collection of data items. Data item is defined in class DataItem. Any data item has two attributes: Name and Color. Also, DataHolder contains a method which allows to search through the data collection in order to find data items which meet a search condition. This method contains two parameters. The first parameter defines search criteria. The second parameter defines a method to call when search criteria is reached.

It is very important to understand that class-server loops all data collection from the start to the end. In case if data item meets the search condition, class-server calls a method defined in class-client. The amount of back calls from class-server to class-client is the same as amount of data items in collection meeting the search criteria.

MainClass is a class-client. It creates instance of DataHolder class (lst) and instance of delegate with type “CallBack”. The delegate instance (procedure01) contains information: what user-defined method (PrintSearchResult) class-server has to call in class-client, and what parameters class-server has to pass to this user-defined method. The list of parameters which are passed to user-defined method of class-client is defined in the delegate declaration. Of course, the list of parameters in user-defined method must exactly meet the list of parameters in delegate declaration.

This is the main idea of C# delegate. I recommend to pass the sample project in debugger in order to get step-by-step review of execution.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Web Developer
Russian Federation Russian Federation
I am 35 years old software developer.

Comments and Discussions

Questionvery good description Pin
Southmountain11-Nov-19 14:21
Southmountain11-Nov-19 14:21 
QuestionDid not clear what is the step to use delegate? Pin
Tony Dong25-Oct-06 15:35
Tony Dong25-Oct-06 15:35 
GeneralThanks ! Pin
LongDauDaiCa12-Apr-06 17:08
LongDauDaiCa12-Apr-06 17:08 
GeneralI cannot understand Pin
bengus:)17-Jun-05 3:35
bengus:)17-Jun-05 3:35 
GeneralOkay Pin
limg30-Nov-04 15:58
limg30-Nov-04 15:58 
GeneralThank you ! Pin
tomamarinov8-Nov-04 4:49
tomamarinov8-Nov-04 4:49 
GeneralI liked it but.... Pin
CyberQ48-Sep-04 5:59
CyberQ48-Sep-04 5:59 

Thanks for the article. It really helped me understand the basic implementation on Delegates.

However, it would be nice to show an implementation of Delegate events which is probably more common.

Creating Custom Events etc..

Maybe another article??


GeneralDid anyone try this? An unmanaged C++ EXE register a callback in a C# DLL and then the C# DLL invokes the callback. Pin
hihint8-Apr-04 0:21
hihint8-Apr-04 0:21 
GeneralRe: Did anyone try this? An unmanaged C++ EXE register a callback in a C# DLL and then the C# DLL invokes the callback. Pin
Bas Steijvers13-Nov-06 3:50
Bas Steijvers13-Nov-06 3:50 
GeneralI am a beginner and this is very unlcear Pin
MatadorG28-Feb-04 16:24
sussMatadorG28-Feb-04 16:24 
Generalnot even a good beginner article Pin
Alex Villagran17-Dec-03 19:29
Alex Villagran17-Dec-03 19:29 
GeneralA good beginner article Pin
synopsislogic18-Aug-03 9:36
synopsislogic18-Aug-03 9:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.