This blog has always been the purpose of: in the simplest way to tell the problem is not complex.
Because I am also very dish so also can't speak too complex hhhhhh ...
So if someday one of the great gods sees something that they feel is a problem, please point it out.
Don't talk more about getting into the chase.
——————————————————————————————————————
The purpose of this article is to clarify what the C # callback function is and when to use it.
Just take the example--
- Using System;
- namespace callbacktest
- {
- Class Program //user layer, perform input and other operations
- {
- static void Main (string[] args)
- {
- Calculateclass cc = new Calculateclass ();
- Functionclass FC = new Functionclass ();
- int result1 = CC. Printandcalculate (2, 3, FC. getsum);
- Console.WriteLine ("Call the Developer's addition function, and return the result after processing:" + result1);
- int result2 = CC. Printandcalculate (2, 3, FC. Getmulti);
- Console.WriteLine ("Call the developer's multiplication function and return the result after processing:" + result2);
- Console.readkey ();
- }
- }
- Class Functionclass //Development layer processing, developers write specific calculation methods
- {
- public int getsum (int A, int b)
- {
- return (A + B);
- }
- public int getmulti (int A, int b)
- {
- Return (A * b);
- }
- }
- #in actual development, the following class will be encapsulated, providing only the function interface. Equivalent to the system bottom
- Class Calculateclass
- {
- Public Delegate int somecalculateway (int num1, int num2);
- An incoming parameter is processed at the bottom of the system, and the method is developed by the developer, and the function provides only the return value after the calculation method is executed.
- public int printandcalculate (int num1, int num2, Somecalculateway cal)
- {
- Console.WriteLine ("System bottom Processing:" + NUM1);
- Console.WriteLine ("System bottom Processing:" + num2);
- Return Cal (NUM1, num2); //Call a reference to an incoming function
- }
- More business logic methods can be encapsulated
- }
- #Endregion
- }
Directly copied into the console project to run.
Operation Result:
The feeling example is quite straightforward. Examples can be read without looking at the following.
Here's a detailed explanation (with some implications for encapsulation):
1 . The Getsum () and Getmulti () two functions in the middle of the Functionclass are called callback functions . You can see that there is no place in the whole program that calls the function in this form, like Getsum, but only as a parameter to another function . such as CC. Printandcalculate (2, 3, FC. Getsum).
The following is the definition of Baidu Encyclopedia:
The callback function is not called directly by the implementing party of the function, but is invoked by another party when a particular event or condition occurs, and is used to respond to the event or condition.
Some students may ask, then why not directly write int result1 = getsum? Wouldn't it be more convenient? This involves encapsulation and real-world development issues ↓
_________________________________
Let's imagine the situation:
Our project is a nuclear weapons controller, assuming that there is no callback function involved. When the user enters and executes Getsum, the project will write into the nuclear emission coefficients, such as direction, and then getsum the launch distance, then launch.
It looks perfect too? No flaws?
No, have you ever thought about the wrong day of operation? In case one day someone entered the wrong, the -1,-2 input into the, and now, it is going to hit the outside to the missile hit his home, the loss is immeasurable.
You may say again, let the programmer at the client (user side) to judge the chant, for example add a sentence if (a>0 && b>0) getsum (A, b);
Have you ever wondered if the programmer's code was written in the wrong line one day? Or send an intern like me to come over and write the code? Are you going to hit your own home again, hhhhhhhh? Therefore, in the actual large-scale application, "write the parameters into nuclear weapons" This operation code is not accessible to all, only the project's core engineers after repeated tests can be applied, and to these parameters and even the results of the necessary judgment, and if you want to modify the code will be carefully considered, As you change, the whole project is changed at the bottom, and all the people who call your function will change the performance.
The above is the meaning of encapsulation. (Of course, the package has other meanings, not to repeat it here)
______________________________________________________________________
2, in the actual development, Calculateclass this class will be encapsulated, for example, to provide a DLL file to you, you invoke the DLL to call the inside parameters or functions. The big guy/Master in the General project will pack the underlying stuff into the DLL, preventing mis-operation from crashing, and making it impossible for the outside world to easily access the underlying stuff.
In this class, public delegate int Somecalculateway (int num1, int num2); This statement declares a delegate, and the Somecalculateway Cal defined later represents that the CAL is a function with two int parameters. Examples and applications of delegates here is not much to say, can understand just fine.
3, in the user layer of the main function, the user can input parameters 2, 3, and then get a developer's method such as Getsum, and then take these as parameters, call the underlying function, and then get the desired effect. This way, whether the user or the developer mistakenly operation, the end will be judged by the underlying function, so that the wrong result will not be executed to cause loss.
Said so much, feel the talk is quite clear, suitable for novice look.
If there is a mistake and I hope you can point out, thank you.
Simple explanation of C # callback function and application example (the simplest explanation, the great God Detour)