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 i
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.
class MyWindow : Window
{
Button _button;
public MyWindow()
{
_button = new Button();
// place the button in the window
_button.Click += MyWindow.ButtonClicked;
}
static void ButtonClicked(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button Clicked");
}
}
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:
class MyWindow : Window
{
Button _button;
int _numClicked = 0;
public MyWindow()
{
this._button = new Button();
// place the button in the window
this._button.Click += this.ButtonClicked;
}
void ButtonClicked(object sender, RoutedEventArgs e)
{
this._numClicked += 1;
MessageBox.Show("Button Clicked " + this._numClicked + " times");
}
}
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:
class MyWindow : Window
{
Button _button;
int _numClicked = 0;
public MyWindow(string localStringParam)
{
string localStringVar = "a local variable";
this._button = new Button();
// place the button in the window
this._button.Click += new RoutedEventHandler(
delegate(object sender, RoutedEventArgs args)
{
this._numClicked += 1;
MessageBox.Show("Param was: " + localStringParam +
" and local var " + localStringVar +
" button clicked " + this._numClicked + " times");
});
}
}
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.
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.
Delagates in c# : it defines the signature of the method which it can invoke.In other words we can say that it wraps the reference of the method which it can call. Below are the uses of delegates :
It supports both static and instance methods.
Below is the explanation how it works internally.
//Here is the declaration of the delegates.
public delegate void DisplayNamme(string name);
at run time CLR creates a class for the delegates as shown below.
public class DisplayNamme : System.MulticastDelegate{
// It is a contructor
public DisplayNamme(Object @object, IntPtr method);
// It is the method with the same prototype as defined in the source code.
public void Invoke(String name);
// This method allowing the callback to be asynchronouslly.
public virtual IAsyncResult BeginInvoke(String name,
AsyncCallback callback, Object @object);
public virtual void EndInvoke(IAsyncResult result);
}
We can see it through ILDasm.exe tool. Use this tool to break the DLL.
Constructor have two parameters: IntPrt refers the name of the method that is passed to the function, and @object refers the reference of the object that is passed to the constructor implicitly.
CLR uses Invoke method of delegates to call the callback method.
Below is the implementation of callback method using delegates.
// Declare Delegates
public delegate void DisplayNamme(string name);
class Program
{
public static void getName(string name)
{
Console.WriteLine(name);
}
public static void ShowName(DisplayNamme dn, string name)
{
// callback method calling. We can call it in two ways.
dn(name);
// or explicitly
dn.Invoke(name);
}
static void Main(string[] args)
{
DisplayNamme delDN = getName;
Program.ShowName(delDN, "CallBack");
Console.ReadLine();
}
}