TodayCodeI made a codereview and found some potential problems/bugs. Many of them are caused by C #-based syntactic sugar that is dazzling and does not understand its deep meaning. Here is an example about the closure of C #, Which is easy for beginners.ProgramCan I write the program running result?
Using system; namespace consoleapplication2 {class program {static void main (string [] ARGs) {var T = new test (); console. writeline ("START 1:"); T. dotask (""); console. writeline ("START 2:"); T. dotask ("", 1); console. writeline ("done. "); console. read () ;}} public class test {private taskexecutor _ taskexecutor; private int _ temp; Public void dotask (string args1, long? Args2 = NULL) {If (_ taskexecutor = NULL) _ taskexecutor = new taskexecutor (); _ temp ++; _ taskexecutor. completed + = (S, e) => console. writeline ("completed! _ Temp = {0}, args2 = {1} ", _ temp. tostring (), args2.hasvalue? Args2.tostring (): "null"); _ taskexecutor. start () ;}} Public Delegate void oncompletedeventhandler (Object sender, eventargs E); public class taskexecutor {public void start () {system. threading. thread. sleep (1000); // execute long task .... oncompleted ();} public event oncompletedeventhandler completed; private void oncompleted () {If (completed! = NULL) {completed (this, new eventargs ());}}}}
The running result is: (don't look at it first, click it again)
All saidNo cultureIt's terrible. Here it is called:Poor FoundationTerrible. C # syntax sugar is really sweet. Anonymous functions, lambda, and closures are really handsome. But if we don't understand them, I'm afraid the code bug output rate will be very high. In fact, JS closures, C # closures, java closures all have the same principle. Lambda is the same for many languages. It's no harm to you. Are you right? The basic knowledge related to this article is available on the Internet and in books. I will not repeat it here. (Note: This article is not suitable for masters and masters. I don't want to waste your precious time either. Please bypass it ~)