Commissioned:
Also known as the agent, the event is also a delegate;
Defined at the outermost of the class
1. Define the delegate
Keyword: delegate
Function Signature: Signature and function remain consistent
When defining a delegate, you define it according to the function.
public delegate int first (int a,int b);
The return type of the method pointed to requires the parameters to be consistent!
2. Defining the delegate variable, pointing to the method
A delegate cannot be instantiated because it is not a class;
First F = new Jiafa; New delegate variable, point to method, note!! method does not need parentheses!!
The second time you can use + =
public int Jiafa (int a,int b)
{
return a+b;
}
Call:
f (5,3);
Can be understood as a pointer to a function, which function the delegate points to, which function is represented by the delegate
Allows functions to be passed as arguments
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace ConsoleApplication1
8 {
9//single row mode
10//Control a class can instantiate only one object
11
//class Test
13//{
/public string name;
15//}
16
17//Data Access class
Class Dbda
19 {
public string host;
public string database;
22
23//static member, used to store objects of this class
public static DBDA db = null;
25
26//Let the class not be instantiated
Private Dbda ()
28 {
29}
30
31//Provide a method of making objects, control can only build one object
public static Dbda Duixiang ()
33 {
if (db = = null)
35 {
db = new Dbda ();
37}
38
The return DB;
40}
41}
42
43//define Delegates
delegate void Suibian (string s);
45
Class Program
47 {
The static void Main (string[] args)
49 {
//Test T1 = new test ();
Wuyi//test t2 = new Test ();
52
//DBDA db = Dbda. Duixiang ();
//db.host = "localhost";
//dbda db1 = Dbda. Duixiang ();
56
57
58//Delegate
59//Parameterization of the method
Suibian s = China;
61
s + = America; + = is the binding method = minus a method
63
64//Event
65//Event is a special kind of delegate
66
67
68//Call speech method
Speak (s);
70
71
72
Console.WriteLine ();
Console.ReadLine ();
75
76
77//Object-oriented three major features
78//design mode
79}
80
81//Method of speech function
Speak static void (Suibian Yu)
83 {
Yu ("Zhang San");
85
//if (n = = 0)
87//{
//America ();
89//}
//else if (n = = 1)
91//{
//China ();
93//}
94//else if (n = = 2)
95//{
//Hanyu ();
97//}
98
99}
100
101//Voice pack
102 public static void America (string s)
103 {
104 Console.WriteLine (s+ "Hello");
105}
106 public static void China (string s)
107 {
108 Console.WriteLine (s+ "Hello");
109}
public static void Hanyu (string s)
111 {
Console.WriteLine (s+ "Bjdkaj");
113}
114
115
116}
117}
SQL Object-oriented (delegate)