[C #] delegate,
1. Basic delegate writing: internal class Program {private static void Main (string [] args) {ChainDelegate (); Console. readKey ();} public static void ChainDelegate () {// create a delegate Feedback feedbackToConsole = new Feedback (FeedbackToConsole); Feedback feedbackToMsBox = new Feedback (FeedbackToMsBox ); // feedbackToConsole (1); feedbackToMsBox (2); // Feedback fbChain = null; fbChain + = feedbackToConsole; fbChain + = feedbackToMsBox; fbChain (3 );} public static void FeedbackToConsole (Int32 value) {Console. writeLine (string. concat ("FeedbackToConsole->", value);} public static void FeedbackToMsBox (Int32 value) {Console. writeLine (string. concat ("FeedbackToMsBox->", value) ;}// define a delegate internal delegate void Feedback (Int32 value );View Code
Knowledge point:
1. Define delegate: delegate void Feedbakc (Int32 value)
- Delegate is a keyword.
- Void return value (various return values can be defined here, including generics)
- Int32 value defines the input parameters.
2. Define the callback function FeedbackToConsole and FeedbackToMsBox
-
- The type and number of input parameters of the callback function must be exactly the same as that of the defined delegate.
- The return value is that the reference type must comply with the covariant and invert parameters (see the generic type I reproduced here). The value type does not need to comply with the covariant and invert parameters.
3. Create a delegate var feedbackToConsole = new Feedback (FeedbackToConsole)
- Use the new Keyword to create a delegate
- The parameter is your callback function (please refer to 2nd points)
4. Call the delegate
FeedbackToConsole (1)
FeedbackToConsole. Invoke (1)
-
- After a delegate is created, the corresponding Object Name (feedbackToConsole) is equivalent to the delegate function (FeedbackToConsole ).
5. Delegated chain
Feed fbChain = null;
FbChain + = feedbackToConsole; fbChain = (Feedback) Delegate. Combine (fbChain, feedbackToConsole );
FbChain + = feedbackToMsBox; fbChain = (Feedback) Delegate. Combine (fbChain, feedbackToMsBox );
-
- Two writing methods are listed. I like the first one, which is simple and straightforward.
- Same as calling a single delegate chain, fbChain (3)
- The call sequence of the delegate chain is the same as that of the bound one (here, feedbackToConsole is called first and then feedbackToMsBox is called)
6. Remove Data from the delegate chain (Supplement)
FbChain-= feedbackToConsole fbChain = (Feedback) Delegate. Remove (fbChain, feedbackToConsole );
- Ii. Delegate and Lamda expressions
Feedback fd1 = new Feedback(i => { Console.WriteLine(i); });
We use the Lamda expression to directly replace the callback function. If the callback function is not used in multiple places, I personally prefer this method, and I will write it as follows:
Feedback fd1 = new Feedback(Console.WriteLine);
This part requires some understanding of the Lamda expression.
- Iii. Generic Delegation
1. The generic delegation is the same as the generic interface. It involves covariant and inverter (here we can see the generic 3 I have reproduced)
2. You do not need to create generic delegation because C # provides two types of delegation:
Action <T,...>: only generic delegation with input parameters (with N reloads)
Func <in TInput,... out TOutput>: generic delegation with input parameters and return values (with N reloads)
3. For more information about EvenHandler, see [C #] events.
[C Language] Is there a function that can clear keys in the cache?
Fflush (stdin)
Clear standard input Cache
# Include "stdio. h"
Main ()
{
Char a, B;
Scanf ("% c", & );
// Fflush (stdin );
Scanf ("% c", & B );
Printf ("\ n % c", a, B );
}
You can try it. If there is no fflush (stdin), if you enter a string of characters "abcd", a = 'A', B = 'B'
If fflush (stdin) exists, after entering "abcd", the program continues to wait for the input, and then enters "efdfsd". The result is a = 'A', B = 'E'
Help [C Language] insert and sort directly
It has been tested and feasible.
# Include "stdio. h"
# Define MAXSIZE 20 // maximum length of a small sequence table used as an Example
Int Insertsort (int r [], int n)
{// Sort by direct insert
Int I, j;
For (I = 2; I <= n; I ++)
{R [0] = r [I]; // r [0] is used as a sentry unit.
J = I-1;
While (r [0] <r [j])
{R [j + 1] = r [j]; // record move-back
J --;
} // While
R [j + 1] = r [0]; // insert to the correct position
For (j = 1; j <= n; j ++) // output the result of each sort.
{
Printf ("% d", r [j]);
} //
Printf ("\ n ");
} //
} // Insertsort
Int main ()
{
Int n, I; // Number of keywords to be sorted
Int r [MAXSIZE];
Scanf ("% d", & n );
For (I = 1; I <= n; I ++) // enter the keyword to be sorted.
Scanf ("% d", & r [I]);
Insertsort (r, n );
}