I`m having some trouble in understanding how delegates in C# work. I have many code examples, but i still could not grasp it properly.
Can someone explain it to me in "plain english"? Of course! examples of code will help, but i think i need more of a description of how/why it works.
EDIT:
Well, the question is: why does delegates work? What is a "flow chart" of the entire process?
What are the pre-requisites of using delegates?
I hope this makes the question clearer.
One way to think about a delegate is like a reference to a function. For example, say you have a button in a window, and you want something to happen when the button is clicked. You can attach a delegate to the Click event of the button, and whenever the user clicks this button, your function will be executed.
Notice how I make ButtonClicked a static function - I want to make a point about non-static functions next. Suppose instead that ButtonClicked is a non-static member:
Now the delegate contains both a reference to the function "ButtonClicked" and the instance, "this", which the method is called on. The instance "this" in the MyWindow constructor and "this" in ButtonClicked are the same.
This is a specific case of a concept known as closures which allows "saving" the state - the current object, local variables, etc. - when creating a delegate. In the above example, we used "this" from the constructor in the delegate. We can do more than that:
Here we created an anonymous delegate - a function which is not given an explicit name. The only way to refer to this function is using the RoutedEventHandler delegate object. Furthermore, this function exists in the scope of the MyWindow constructor, so it can access all local parameters, variables, and the member instance "this". It will continue to hold references to the local variables and parameters even after the MyWindow constructor exits.
As a side note, the delegate will also hold a reference to the object instance - "this" - even after all other references to the class a removed. Therefore, to ensure that a class is garbage collected, all delegates to a non-static member method (or delegates created in the scope of one) should be removed.
A Delegate is a reference type variable that points the reference to a method. All delegates are derived from System.Delegate class. For example in Windows Forms or WPF, a method event works with the concept of delegates. This is an example of using delagates in C# Introduction to delegates in C#
Delegates are reference type, a delegate refers to a method. This is called encapsulating the method. When you create a delegate you specify a method signature and return type. You can encapsulate any matching method with that delegate. You create a delegate with the delegate keyword, followed by a return type and the signatures of the methods that can be delegated to it, as in the following:
Output is Hello from Delegate
1) First you have to understand why/when you need a delegate, what is the problem that it solve.
In my experience I mainly use them to allow a user to customize an object's behavior.
Immagine a Grid component that allows the developer to customize how each column will be rendered. For example you want to write a of red color value when it's a number under zero.
The developer that create the Grid does not know how the user want to customize the output so it need a mechanism that let the user of the component to inject some logic into the component.
2) Then you have to understand how the delegate works
What is confusing is the strange code you have to write to do so, and the many ways you have to do the same thing.
This is the grid class:
Note that is like a normal method but:
In this way I say: "the method that will format the output has this interface: it will take a string as input and it will output a string"
It remember to me a definition of a method of an interface.
Now I have to create a property of the type of the delegate that will handle te implementation of this method.
Inside the Print method I can use the handler that will link the real implementation.
ValueFormatterHandler is of type ValueFormatterDelegate and ValueFormatterDelegate is ad delegate and .Invoke is a method of delegate type
This is a program that use my Grid class and is able to personalize it on the fly. The problem here is the many ways you have to do the same thing.
Its an inversion principle. Normally, you write code that calls a method and the method you call is known at the time that you write the code. Delegates allow you to anonymously invoke methods. That is you do not know the actual method that is called when the program is running.
It is useful in separating the concerns of different parts of an application. So you can have some code that performs tasks on a data store. You may have other code that processes the data. The processes on the data need not know the structure of the data store and the data store should not be dependant on the uses of the data.
The processing code can be written assuming certain things about the data that are independant of the structure of the data store. That way, we can change the structure of the data store with less worry about affecting the processes on the data.
Well, a delegate is a type. The variables of a delegate type can reference, or point to, a function.
This gives you an indirect way to call a method, so methods can be selected at runtime. And thus you can have variables, parameters and properties containing a method. The properties are called events.
Just one more code sample, to be complete:
Use of delegate variables is not common. But you can use a delegate parameter to for instance a Sort method to select an ascending or descending sort.
And you probably know events, which are (a special kind of) properties based on delegates.