Delegates in C#

后端 未结 9 1355
刺人心
刺人心 2020-12-13 04:13

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

相关标签:
9条回答
  • 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#

    0 讨论(0)
  • 2020-12-13 04:55

    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";
        }
    
    }
    
    0 讨论(0)
  • 2020-12-13 04:59

    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.

    0 讨论(0)
  • 2020-12-13 05:00

    You can think of delegates as a way to view code as data. If you create a delegate, it is a type. Variables of this type may point to specific methods (that comply with the delegate definition).

    That means that you can treat a piece of code as data and for instance pass it to a method. Since delegates point to code (or null), you may also invoke the code that it points to via the variable.

    That allows for some very useful patterns. The classic example is how to sort a collection. By allowing the caller to supply a delegate that implements what it means to sort specific elements, the sorting method doesn't have to know anything about this.

    The same idea is used extensively with a lot of methods for LINQ. I.e. you pass in a delegate (or more commonly a lambda) which handles some specific task, and the LINQ method in question will call this in order to complete the task.

    0 讨论(0)
  • 2020-12-13 05:01

    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

    0 讨论(0)
  • 2020-12-13 05:02

    A delegate is a references type that invokes single/multiple method(s)through the delegate instance. It holds a reference of the methods.Delegates can be used to handle(call/invoke) multiple methods on a single event. Delegates can be used to define asynchronous methods. Here is an example for delegate First we create a class.In which we declare the delegate.and we create a method inside the class in which we invoke the delegate.

    public class simpleinterest
    {
        public delegate void intcal(double i);  //declare delegate
        public event intcal interest; //create delegate object
        public void calculate(double p, double n,double r)
        {
           interest(p*n*r/100);   //invoke delegate
        }
    
    }
    

    Inside our program we do the mapping.That is we specify which event will trigger when invoke the delegate.

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            simpleinterest s1 = new simpleinterest();
            s1.interest+=new simpleinterest.intcal(s1_interest);//mapping
    
            s1.calculate(1000,3,10);
    
        }
        void s1_interest(double r)
        {
             MessageBox.Show("Amount:" + r.ToString());
    
        }
    
    0 讨论(0)
提交回复
热议问题