Delegates In C# - The Definition

Posted by Ahmed Tarek Hasan on 9/05/2012 08:28:00 PM with No comments

Delegates In C# - The Definition

What does the word "delegate" mean?
The word "delegate" in English means: {A person authorized to act as representative for another; a deputy or an agent} or {A representative to a conference or convention} or {To give a particular job, duty, right, etc. to someone else so that they do it for you}

What does "delegate" mean in C#?
Back from the days of C++, the main concept of delegates existed but with another name; pointer to function.  Actually, the name pointer to function may seem more appropriate and descriptive. When we think of a pointer to function in C#, we may prefer to think of it as a reference to a function. But, these are just some technical words which mean nothing to someone really needs to understand the main concept of delegates. For now, for simplicity, let's think of delegates as if they are the way by which we can postpone thinking of and writing the logic/code of some part/module inside our main code. Why would we need to postpone writing this code ........... wait and see :)

Why do we need "delegates"? What are advantages of using "delegates"?
Using delegates has many advantages but i think the most important ones are:
  • Separation of concerns: Assume you are writing the code for a windows forms user control. This control includes a slider control which will be used to affect gradually something else on the main form when your custom control is dragged and dropped on it. While writing your code of the custom control, all what concerns you is how to manage all aspects of the custom control, how to bind internal events, how to make your custom control resizeable, how to expose some extra properties,......... and so on. But, you are not concerned by any way with how the user will use your control to change the color of the main form background when changing the slider position on your slider, or, how he will use it to raise up the volume of a media player ............. and so on of infinite possibilities of how the user can use your custom control. So, by using delegates, you will separate the concerns to only focus on the logic related to your piece of art, nothing else.
  • Encapsulation: Assume the same example above, when you are writing the code for the same custom control, you can't know exactly how the user will use your control (background color, volume,.....) because you have endless possibilities. Also, it is not logical at all to write your code to only work for one possibility (usage) because you need your custom control to be widely used in different applications and to minimize the restrictions as much as possible. So, by using delegates, you will postpone the logic/code of the action to be taken when the user moves the slider on your custom control ........ the user himself will decide the logic and write this code. Finally, this caused you to encapsulate your piece of art.
When to use delegates?
To answer this question, you need to ask yourself if you are working on a code and you faced some logic you feel that it doesn't belong in your scope or you feel that this logic may differ according to the one who will use your code, or even you. Sometimes, you are both the developer and user of your code, does this mean that you don't need to postpone writing some code using delegates????

What if no stranger will use my code, do I still need to use delegates?
Yes. Sometimes you will need to use delegates even if you are the only user of your code because you don't need your code to be unbalanced and unstable, and the most important is that you need your code to be extendable and maintainable. You should not write some logic and code of some certain module inside a totally different module because this will cost you much time and effort to maintain and while fixing bugs and at some point your code will collapse. So, you need to know how to use delegates in the right place.

When can I use delegates?
There are many cases where you can use delegates some of them you have already seen but didn't recognize. Some of these cases are:
  1. In Windows Forms Applications, .Net built in controls (button, list box, drop down list, ....): In these control you already have been using delegates for a long time. Like when you double click on a button control, you get an empty method where you can write some code to be executed when the button is clicked at run-time. This empty method is a delegate which is fired internally by the button when a user clicks on the button. Imagine if the button control didn't use a delegate to decide the code that it will execute when it is clicked, if this happened then there should have been a button control for every action/code/logic that anyone could need in his application ........... this is impossible and impractical.
  2. Custom controls: You can design your custom control and "delegate" the code that will be executed when users interact with your custom control, you don't have to write a certain static logic.
  3. Libraries: You can write your own library that handle some complicated logic and compile your library into a DLL to be used by all users all around the world. At some point in your code, you will need to write some logic/code which is out of the scope of your library and which can take endless forms. Now, this is the right time to "delegate" this logic to the library user so that he can decide the logic which he sees fitting. Example: a library to sort any type of objects in a list, at some point you will need to compare two object instances in the list to decide which of them is bigger, but you don't know the type of the object while writing your code, also, even if they are of type int, who said that the library user will decide that 1 is bigger than 2? may be he will apply some sort of mathematical operations to compare each two numbers ............. So, anyway, it is not up to you to decide how to compare the elements in the list.
  4. Custom events: When you start using delegates, you will find that they are required when you decide to write your own events for your custom class. This should be understandable because when you write your event you just write a logic to raise a flag when some action happens, but, you don't write the logic/code to be executed when this flag is raised.
  5. Many other applications.
Can I see some code on how to define and use delegates?
Right now this is out of scope of this article but i will try to provide some code on how to use delegates in more than one form. This may be in another article.
I already posted an article on how to use delegates in windows forms applications controls. You can check Events & Delegates In C# - Win Forms Controls


I wish this helped you to understand some of the basic concepts of delegates and I encourage you to search more and read about delegates.

Good Luck :)




Categories: , ,