Simple explanation of C # callback function and application example (the simplest explanation, the great God Detour)

Source: Internet
Author: User

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--

  1. Using System;
  2. namespace callbacktest
  3. {
  4. Class Program //user layer, perform input and other operations
  5. {
  6. static void Main (string[] args)
  7. {
  8. Calculateclass cc = new Calculateclass ();
  9. Functionclass FC = new Functionclass ();
  10. int result1 = CC. Printandcalculate (2, 3, FC. getsum);
  11. Console.WriteLine ("Call the Developer's addition function, and return the result after processing:" + result1);
  12. int result2 = CC. Printandcalculate (2, 3, FC. Getmulti);
  13. Console.WriteLine ("Call the developer's multiplication function and return the result after processing:" + result2);
  14. Console.readkey ();
  15. }
  16. }
  17. Class Functionclass //Development layer processing, developers write specific calculation methods
  18. {
  19. public int getsum (int A, int b)
  20. {
  21. return (A + B);
  22. }
  23. public int getmulti (int A, int b)
  24. {
  25. Return (A * b);
  26. }
  27. }
  28. #in actual development, the following class will be encapsulated, providing only the function interface. Equivalent to the system bottom
  29. Class Calculateclass
  30. {
  31. Public Delegate int somecalculateway (int num1, int num2);
  32. 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.
  33. public int printandcalculate (int num1, int num2, Somecalculateway cal)
  34. {
  35. Console.WriteLine ("System bottom Processing:" + NUM1);
  36. Console.WriteLine ("System bottom Processing:" + num2);
  37. Return Cal (NUM1, num2); //Call a reference to an incoming function
  38. }
  39. More business logic methods can be encapsulated
  40. }
  41. #Endregion
  42. }
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.