Delegates in C#

2020-02-02 11:09发布

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.

标签: c# delegates
9条回答
Luminary・发光体
2楼-- · 2020-02-02 11:29

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.

查看更多
再贱就再见
3楼-- · 2020-02-02 11:31

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#

查看更多
成全新的幸福
4楼-- · 2020-02-02 11:35

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:

public delegate void HelloFunctionDelegate(string message);
class Program
{
 static void Main()
{
HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);
del("Hello from Delegate");
}
public static void Hello(string strMessage)
{
 Console.WriteLine(strMessage);
}
}

Output is Hello from Delegate

查看更多
【Aperson】
5楼-- · 2020-02-02 11:40

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:

// the grid let the programmer that will use it to customize the output
public class Grid{

    // 1) First I declare only the interface of the delegate
    public delegate String ValueFormatterDelegate(String v);

    // 2) I declare a handler of the implementation of the delegate
    public ValueFormatterDelegate ValueFormatterHandler; 

    // 3) I call the handler inside the Print method
    public void Print(String x){
        Console.WriteLine( ValueFormatterHandler.Invoke(x) );
    }

}

// 1) First I declare only the interface of the delegate public delegate String ValueFormatterDelegate(String v);

Note that is like a normal method but:

  • it has a delegate keyword
  • it don't has a implementation

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.

// 2) I declare a handler of the implementation of the delegate public ValueFormatterDelegate ValueFormatterHandler;

Now I have to create a property of the type of the delegate that will handle te implementation of this method.

// 3) I call the handler inside the Print method public void Print(String x){ Console.WriteLine( ValueFormatterHandler.Invoke(x) ); }

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.

using System;

public class Program{
    public static void Main(){

        var printer = new Printer();

        // METHOD 1 : link to a named method
        // here i link the handler of the delegate to a real method
        // the FormatXXX is a static method defined at the ed of this code
        printer.ValueFormatter = FormatXXX;

        // when i call Print("hello")
        printer.Print("hello"); // XXhelloXX

        // METHOD 2 : anonimous method
        // think at this like a method but without a name
        // FormatYY (String x ){ return "YY"+x+"YY"; };
        //  become
        // delegate (String x ){ return "YY"+x+"YY"; };
        printer.ValueFormatter = delegate (String x ){ return "YY"+x+"YY"; };
        printer.Print("hello"); // YYhelloYY

        // METHOD 3 : anonimous method using lambda
        // as you can note the type of parameter x is inferred from the delegate declaration
        // public delegate String ValueFormatterDelegate(String v);
        printer.ValueFormatter = (x)=>"KK" + x + "KK";

    }

    public static String FormatXXX(String y){
        return "XX"+ y +"XX";
    }

}
查看更多
祖国的老花朵
6楼-- · 2020-02-02 11:40

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.

查看更多
Ridiculous、
7楼-- · 2020-02-02 11:44

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:

   delegate void ADelegate();  // the delegate type

   void Foo() { ... }   // a compatible method
   void Bar() { ... }   // a compatible method

   void Main()
   {
      ADelegate funcPtr;  // a delegate variable

      if (aCondition)
        funcPtr = Foo;  // note: _not_ Foo(), Foo is not executed here
      else
        funcPtr = Bar;

      funcPtr(); // calls Foo or Bar depending on aCondition
   }

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.

  delegate int Compare(MyClass a, MyClass b);  // the delegate type

  void int CompareUp(MyClass a, MyClass b) { ... }   
  void int CompareDn(MyClass a, MyClass b) { ... }   

  void Sort(MyClass[] data, Compare comparer) { ... }

And you probably know events, which are (a special kind of) properties based on delegates.

查看更多
登录 后发表回答