Click here to Skip to main content
15,890,336 members
Articles / Programming Languages / C#
Article

Delegate: What is this?

Rate me:
Please Sign up or sign in to vote.
2.97/5 (43 votes)
17 Aug 20032 min read 105.1K   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.

License

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 13:21
Southmountain11-Nov-19 13:21 
QuestionDid not clear what is the step to use delegate? Pin
Tony Dong25-Oct-06 14:35
Tony Dong25-Oct-06 14:35 
GeneralThanks ! Pin
LongDauDaiCa12-Apr-06 16:08
LongDauDaiCa12-Apr-06 16:08 
GeneralI cannot understand Pin
bengus:)17-Jun-05 2:35
bengus:)17-Jun-05 2:35 
GeneralOkay Pin
limg30-Nov-04 14:58
limg30-Nov-04 14:58 
GeneralThank you ! Pin
tomamarinov8-Nov-04 3:49
tomamarinov8-Nov-04 3:49 
GeneralI liked it but.... Pin
CyberQ48-Sep-04 4:59
CyberQ48-Sep-04 4:59 
GeneralDid anyone try this? An unmanaged C++ EXE register a callback in a C# DLL and then the C# DLL invokes the callback. Pin
hihint7-Apr-04 23:21
hihint7-Apr-04 23: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 2:50
Bas Steijvers13-Nov-06 2:50 
GeneralI am a beginner and this is very unlcear Pin
MatadorG28-Feb-04 15:24
sussMatadorG28-Feb-04 15:24 
Generalnot even a good beginner article Pin
Alex Villagran17-Dec-03 18:29
Alex Villagran17-Dec-03 18:29 
GeneralA good beginner article Pin
synopsislogic18-Aug-03 8:36
synopsislogic18-Aug-03 8:36 
Thanx for this article. Do not worry about your english! The mean thing is that we understand what you want to explain ... and you have reached your goal Smile | :)

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.