当前位置:网站首页>C 9.0 new features explain series 4: top level programs

C 9.0 new features explain series 4: top level programs

2020-12-07 02:43:27 itread01

## 1 Background and motivation Usually , If you just want to use C# Print a line on the console “Hello World!”, It's not Console.WriteLine("Hello World!"); It can be done in one sentence , It also involves other necessary base code ( Such as defining classes and entry functions Main), For example, the following : ```C# using System; class Program { static void Main() { Console.WriteLine("Hello World!"); } } ``` Just print a sentence “Hello World!”, So much code . This is not only a trouble for beginners , Make the code messy and , And added the indent level . To solve these problems , The new feature of top-level code statement is proposed . ## 2 Top level statement ### 2.1 Introduce stay C#9.0 in , Will Class The definition and main function of Main Omit the announcement of , Just write your core business code , It's a top-level statement . The above code , We can use top-level statements to write as : ```C# using System; Console.WriteLine("Hello World!"); ``` So , The code is concise and clear , Easy for beginners to understand . Isn't it a little bit written Python The feeling of ? Of course , Any statement is allowed . If you want to return the value , You can do that ; You want to use it? await, You can do that too ; If you want to access command-line arguments ,args It's also available ; You want to use local functions , It's OK, too . Although you can use any code , But there are rules that must be followed : * Top level statements must be placed in using After the statement code * Top level statements must be used before any type or namespace declaration * Top level statements can only be written in a source file , Like today, I can only write one main The same way . * Local functions and variables defined in the top level statement , Calling them anywhere outside the top-level code segment will result in an error . The following code is a good example : ```C# using static System.Console; using System.Threading.Tasks; WriteLine("Hello,"); Print(args[0]); await Task.Delay(1000); return 0; void Print(string arg) { WriteLine(arg); } ``` ### 2.2 principle We know ,C# As an object-oriented programming language , All types are object-oriented , Have type and member definitions . Top level statements seem to violate this rule , Actually, there is no . This is because , The top-level statements are finally compiled , It is regarded as the whole life space Program Class Main A piece of code in the method body is automatically generated together . As shown below : ```C# static class Program { static async Task Main(string[] args) { // Top level statement } } ``` It should be noted that , The class name here Program And method name Main Just for example , In fact, the compiler does not generate this name . We can examine IL The code confirms this : ![IL Screenshot ](https://mmbiz.qpic.cn/mmbiz_png/LibgAhqUgqnrqd5CAxKdd4l0KEuEF3IAO6szLnWcJ9XhegEicB3jHt934G5dsRs5GXo9RqNOtHq2LsS8RaetloNw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1) Depending on whether there are asynchronous operations and return values in the top-level statement , The generated entry function signature is also different . The details are shown in the table below : || There are return values | There is no return value | |---|---|---| | There is asynchrony |async static Task\ Main(string[] args)|async static Task Main(string[] args)| | There is no asynchrony |static int Main(string[] args)|static void Main(string[] args)| For example, the code above , Generated entry function \ $ As shown in the following code : ```C# static class Program { async static Task Main(string[] args) { WriteLine("Hello"); Print(args[0]); await Task.Delay(1000); return 0; void Print(string arg) { WriteLine(arg); } } } ``` ## 3 Conclusion Using top-level statements can simplify our coding work , Make the code look simple and clear , Friendly to beginners, too , It doesn't change in essence C# The original grammatical structure of the language of , Any sentence can use , There are no additional restrictions , From these aspects , It's a positive change . [1]: https://mp.weixin.qq.com/s?__biz=MzAwNjcyNTU2Ng==&mid=2247483802&idx=1&sn=e1d3b933223df6de0f9f3d7e1200a93f&chksm=9b084b4bac7fc25da689cf11baf6e073316c49be817289d82aeb3566b6f724b3e2cbf5a44dd0&cur_album_id=1612459507345899521&scene=189#rd #### If it is of value to you , Please recommend , Your encouragement is my motivation to continue , Thank you very much . Pay attention to my public account “ Code guest wind and cloud ”, Enjoy the first time to read the latest articles .

 

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207024030514q.html