下面来看一个控制台应用程序示例(ConsoleApplication1),研究一下它的结构。其代码如下所示:
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace ConsoleApplication1
6 {
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 //out text to the screen.
12 Console.WriteLine("hello!");
13 Console.ReadKey();
14 }
15 }
16 }
17
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace ConsoleApplication1
6 {
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 //out text to the screen.
12 Console.WriteLine("hello!");
13 Console.ReadKey();
14 }
15 }
16 }
17
可以立即看出,上一节讨论的所有语法元素这里都有。其中有分好、花括号、注释和适当的缩进。
看一下代码中最重要的部分:
static void Main(string[] args)
10 {
11 //out text to the screen.
12 Console.WriteLine("hello!");
13 Console.ReadKey();
14 }
在运行控制台应用程序时,就运行这段代码,更准确的说,是运行花括号中的代码块。注释行不做任何事情。其他两行代码在控制台窗口中输出了一些文本,并等待一个响应。目前我们不必关心它的具体机制。