I've been asked during the interview. What is a delegation? Is an event a delegation? What are the advantages of delegation? I often use it in projects, but usually I don't pay attention to sorting out the conceptual knowledge, and the answer is like swallowing the whole thing, so I can't answer it. Take some time out this weekend and settle down. Next, I will use a question and answer nature to sort out and record.
1. What is delegation?
A delegate is a type safe object that points to a function method (which can be multiple) in the program that will be called later.
2. Is the event a delegation?
Yes, it's a special kind of commission.
3. How to create a delegation?
1 // The delegation type contains three main information: 2 // 1.The name of the method it calls. 3 // 2.Optional parameter to call the method. 4 // 3.The return type of the method (optional). 5 public delegate string Texts(int x, int y);
4. What are the nature of entrustment?
There are two types of delegation: asynchronous delegation and synchronous delegation.
5. After creating a delegate, what happens when the program runs?
When the compiler processes the delegate object, it will automatically generate a sealed class derived from System.MulticastDelegate and its base class System.Delegate. Together to provide the necessary infrastructure for the Commission.
Through ildasm.exe, we look at the text delegate we just created and find that it defines three public methods.
1 public string Invoke(int x, int y); 2 public IAsyncResult BeginInvoke(int x, int y, AsynCallback cb, object state); 3 public int EndInvoke(IAsyncResult result);
You can see that BeginInvoke is used for asynchronous calls.
6. Can you write an example of actual combat?
1 class Program 2 { 3 //Declare a delegate that can point to any two passed in int Type parameter, and return int Method of type 4 public delegate int Texts(int x, int y); 5 public class SimpleMath 6 { 7 public static int Add(int x, int y) 8 { 9 return x + y; 10 } 11 public static int Subtract(int x, int y) 12 { 13 return x - y; 14 } 15 16 } 17 static void Main(string[] args) 18 { 19 //Create a point SimpleMath In class Add Object of method 20 Texts t = new Texts(SimpleMath.Add); 21 22 //Indirect call with delegate object Add Method 23 Console.WriteLine("2+3={0}", t(2, 3)); 24 Console.ReadLine(); 25 26 } 27 }
7. What is entrusted multicast.
Delegate multicast can be understood as creating a delegate object that maintains a list of callable methods rather than just a single method. Code directly may be more intuitive.
1 class Program 2 { 3 //Declare a delegate that can point to any two passed in int Type parameter, and return int Method of type 4 public static int a = 0; 5 public delegate int Texts(int x, int y); 6 public class SimpleMath 7 { 8 public static int Add(int x, int y) 9 { 10 return a = x + y; 11 } 12 public static int Subtract(int x, int y) 13 { 14 return a = x + y + a; 15 } 16 17 } 18 static void Main(string[] args) 19 { 20 //Create a point SimpleMath In class Add Object of method 21 Texts t = new Texts(SimpleMath.Add); 22 //Entrusted multicast, which can be used directly+= 23 t += SimpleMath.Subtract; 24 t(2, 3); 25 //Indirect call with delegate object Add and Subtract Method 26 Console.WriteLine("a=" + a + ""); 27 Console.ReadLine(); 28 29 } 30 }
8. Is there a way to quickly create a delegation?
We can use action < > and func < > to quickly create delegates.
1 public static void TextAction(int x, int y) 2 { 3 Console.WriteLine("x=" + x + ",y=" + y + ""); 4 } 5 static void Main(string[] args) 6 { 7 8 //Use Action<>Generic delegate creates a point quickly TextAction Method (attention) Action Can only point to methods with no return value) 9 Action<int, int> action = new Action<int, int>(TextAction); 10 action(1, 2); 11 12 }
1 public static int Textfun(int x, int y) 2 { 3 return x + y; 4 } 5 static void Main(string[] args) 6 { 7 8 //Use fun<>Generic delegate creates a point quickly Textfun Method (attention) Func The last parameter value is the return value type of the method) 9 Func<int, int, int> fun = new Func<int, int, int>(Textfun); 10 Console.Write(fun.Invoke(2, 3)); 11 12 }
For the time being, we need to sort out so many mistakes and omissions. You are welcome to point out and add.