Reading directory
I. Delegation and thread
Ii. Delegated instance
Iii. Running Effect
I. Delegation and thread
. Commission Basis
-Purpose of delegation: To pass functions as parameters
-Similar to function pointers in C ++, function pointers are different from function pointers: function pointers can only reference static methods, while delegation can reference static methods or instance methods, when a delegate references an instance method, the delegate not only stores the reference to the method entry point, but also stores the instance reference to call this method.
-Is the basis for event processing.
-Delegate Declaration: delegate int MyDelegate (int I); int indicates the function return type, MyDelegate indicates the delegate name, And I indicates the function parameter.
Ii. Delegated instance
1: declare Delegation
Delegate int MyDelegate (int I );
2: Define a static method and return the product of two numbers
Public static int DelegateMethod (int I)
{
Return I * I;
}
3: declare a delegate variable mydelegate and bind it to the static method DelegateMethod.
MyDelegate mydelegate = new MyDelegate (DelegateMethod );
Instance
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace _ 3_DelegateUse
{
Class Program
{
// Step 1: Delegate www.2cto.com
Public delegate int MyDelegate (int I );
Static void Main (string [] args)
{
// Step 3 declare a delegate variable mydelegate and bind it to the static method DelegateFunction
MyDelegate mydelegate = new MyDelegate (DelegateFunction );
Console. Write ("enter a number :");
Int I = Int32.Parse (Console. ReadLine ());
// Call the delegate method DelegateFunction
Int intResult = mydelegate (I );
Console. WriteLine ("Result:" + intResult );
Console. ReadLine ();
}
// Step 2 defines a static method and returns the product of two numbers.
Public static int DelegateFunction (int I)
{
Return I * I;
}
}
}
Iii. Running Effect
Learn a little bit every day, make a little progress every day, record work with words, and record life with words
From childhood sleepy