Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegates in C#

Tags:

c#

delegates

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.

like image 799
George Silva Avatar asked Nov 14 '09 18:11

George Silva


People also ask

What is delegates and its types?

Definition. A delegate(known as function pointer in C/C++) is a references type that invokes single/multiple method(s) through the delegate instance. It holds a reference of the methods. Delegate types are sealed and immutable type.

What is an example of delegates?

An example of delegate is when you tell someone to get your mail for you. The definition of a delegate is a representative authorized to speak or act for others. An example of a delegate is a politician who speaks on behalf of a group of people. One who acts on behalf of one or more others in an official capacity.

What is the function of a delegate?

A delegate is a type-safe function pointer that can reference a method that has the same signature as that of the delegate. You can take advantage of delegates in C# to implement events and call-back methods. A multicast delegate is one that can point to one or more methods that have identical signatures.

What are delegates in an event?

A delegate is a type that holds a reference to a method. A delegate is declared with a signature that shows the return type and parameters for the methods it references, and it can hold references only to methods that match its signature. A delegate is thus equivalent to a type-safe function pointer or a callback.


1 Answers

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.

like image 125
Anton Avatar answered Sep 30 '22 20:09

Anton