导入
C# 程序的结构
理解 C# 程序的基本结构,包括语句、代码块、Main 入口点和注释。
一个简单的 C# 语句
1Console.WriteLine("This is a statement.");
定义代码块
1{ 2 Console.WriteLine("I am inside a block!"); 3 Console.WriteLine("Me too."); 4}
1using System; 2 3class FirstProgram 4{ 5 static void Main() 6 { 7 Console.WriteLine("Hello, World!"); 8 9 { 10 Console.WriteLine("Code inside a block"); 11 { 12 // Nested block 13 Console.WriteLine("Code inside an enclosed block"); 14 } 15 } 16 } 17}
...
Code block diagram
1using System; 2 3class FirstProgram 4{ 5 static void Hello() 6 { 7 Console.WriteLine("An executable .NET app cannot be compiled without the Main method"); 8 } 9}
代码示例
1error CS5001: Program does not contain a static 'Main' method suitable for an entry point
1using System; 2 3/* 4 A program that prints to the console. 5*/ 6class Program 7{ 8 // The Main method (the entry point) 9 static void Main() 10 { 11 Console.WriteLine("I am 'Program' class"); 12 } 13}
1Console.WriteLine("I am a top-level program");