步入C#--hello world

从《The C Programming Language》开始,每一门语言都会以Hello world开篇,像朋友间打招呼一样,身为程序员的我们,也许真的要和世界打个招呼了。

作为C#的第一个程序,创建一个控制台应用程序输出“hello world”再合适不了。

首先新建项目,创建一个控制台应用程序;

创建好后将会在Program.cs文件里出现默认的程序;

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Hello_world
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             
13         }
14     }
15 }

只需要在Main函数内添加代码,即可完成“hello world”的输出;

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Hello_world
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Console.WriteLine("Hello world");
13         }
14     }
15 }

其结果为:

也许很多初学者会像我一样,启动程序后只会看到这个黑色的控制台一闪而过,两个方法解决:

1,不利用绿色箭头或F5启动程序,改用Ctrl+F5启动程序,将会使控制台保持,输出结果如上图;

2,利用绿色箭头或F5启动程序,但是在代码最后使用Console.ReadLine()方法;

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Hello_world
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Console.WriteLine("Hello world");
13             Console.ReadLine();
14         }
15     }
16 }

其输出结果为:

与方法1输出结果不同在于没有Press any key to continue. . .

除此以外,对于一个简单的hello world程序,我们还应简单了解以下一些内容:

1,类与对象:类是对成员的一种封装,对象是实例化后的类型。前者是抽象的,后者是具体的。对象的核心特征是拥有了一份自己特有的数据成员拷贝。它们的区别可以简单理解为对象是实例化后的类。

2,Main方法:Main方法是程序的入口,可以简单理解为程序第一行运行的就是Main方法。C#程序中有且只有一个Main方法,Main方法必须是静态的。

原文地址:https://www.cnblogs.com/LiuYujie/p/3841886.html