Click here to Skip to main content
Click here to Skip to main content

Delegate: What is this?

, 17 Aug 2003
Rate this:
Please Sign up or sign in to vote.
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

About the Author

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

Comments and Discussions

 
QuestionDid not clear what is the step to use delegate? PinmemberTony Dong25-Oct-06 14:35 
GeneralThanks ! PinmemberLongDauDaiCa12-Apr-06 16:08 
GeneralI cannot understand Pinmemberbengus:)17-Jun-05 2:35 
GeneralOkay Pinmemberlimg30-Nov-04 14:58 
GeneralThank you ! Pinmembertomamarinov8-Nov-04 3:49 
GeneralI liked it but.... PinmemberCyberQ48-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. Pinmemberhihint7-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. PinmemberBas Steijvers13-Nov-06 2:50 
GeneralI am a beginner and this is very unlcear PinsussMatadorG28-Feb-04 15:24 
Generalnot even a good beginner article PinmemberAlex Villagran17-Dec-03 18:29 
GeneralA good beginner article Pinmembersynopsislogic18-Aug-03 8:36 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 18 Aug 2003
Article Copyright 2003 by Bachan1968
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid