C# Advanced Analysis of Delegates and Events

From: Blog Garden - Halfway Alone

Original address: http://www.cnblogs.com/banluduxing/p/8999428.html

 This article is reproduced from http://www.cnblogs.com/banluduxing , please indicate the source.

 

What is entrustment

The delegate keyword is added, and there is no method template (method list) of method body {};

public  void Write()
        {
            // TODO 
        }
 // Add the keyword delegate and remove the method body{} 
public  delegate  void Write();

use of delegation

Delegate basics use trilogy: 1 Initialize the declared delegate 2 Bind the method defined 3 Use

The parameter signature of the bound method and the declared delegate must be the same (number of parameters, corresponding type, return type)

 class Program
    {
        // Define method 
        public  static  void WriteFun()
        {
            // TODO 
        }
         public  static  void GetFun( int y)
        {
            // TODO 
        }
         public  static  string SetFun()
        {
            return "";
        }
        //声明委托
        public delegate void Write();
        public delegate void Get(int x);
        public delegate string Set();

        static void Main(string[] args)
        {
            // Initialize the delegate, binding method 
            Write write = new Write(WriteFun);
             // Can be abbreviated Write write = WriteFun; 
            Get get = GetFun;
            Set set = SetFun;
            //直接使用或者用invoke()
            write();//write.Invoke();
            get(3);//get.Invoke(3);
            string str = set();//string str = set.Invoke();
        }
    }

multicast delegation

As mentioned above, a delegate is a list of methods: a delegate can bind multiple methods at a time, and bind and unbind by += and -=;

 class Program
    {
        public static void WriteFun()
        {
            //TODO
        }
        public static void WriteFun1()
        {
            //TODO
        }
        public static void WriteFun2()
        {
            // ALL 
        }

        public delegate void Write();

        static void Main(string[] args)
        {
            Write write = WriteFun;
            write += WriteFun;
            write -= WriteFun;
            write += WriteFun1;
            write += WriteFun2;
            write.Invoke();
        }
    }

Delegates and Events

A delegate is a type , and an event is a delegate instance with the keyword event added

Compared with the above ordinary delegate instance, the event is more special, it cannot be initialized and cannot be called externally

public class Button
    {

        public ButtonClickHandle ButtonClickHandle;
        public event ButtonClickHandle ButtonClickHandleEvent;

        public void click()
        {
            if (ButtonClickHandle !=null)
            {
                ButtonClickHandle.Invoke();
            }
            if (ButtonClickHandleEvent != null)
            {
                ButtonClickHandleEvent.Invoke();
            }
        }
    }
    public delegate void ButtonClickHandle();

The difference is as follows

 Can not be initialized and externally called, this is a security mechanism for permission control

PS

Delegates do not need to be implemented by yourself. The officially provided Action and Func delegates with or without return are sufficient.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325623181&siteId=291194637