目录
C#的第一个程序 Hello World!
/*
* C#的第一个程序 Hello World!
*
* HelloWorld.cs
*
*/
using System;
class HelloWorld{
public static void Main(){
Console.WriteLine("Hello World!");
}
}
手动编译
命令行编译:
csc hello.cs
运行hello.exe:
./hello
Hello, World
.Net Core下的HelloWorld
创建控制台项目
dotnet new console -o hello
创建好后默认就是一个Hello World的控制台程序,进入项目目录直接运行:
cd hello
dotnet run
使用Visual Studio 2019
创建项目
-
打开isual Studio 2019.
-
在起步窗户中选择 Create a new project.
-
在 Create a new project 窗口的搜索框中输入 console 。接着在语言列表中选择 C#,然后在平台列表中选择Windows 。
在过滤结果中选择 Console App (.NET Core)模板,点击下一步。
-
在Configure your new project 窗口中,Project name 输入 HelloWorld ,点击 Create.
Visual Studio 打开一个新的项目
Create the application
Visual Studio 的项目中包含一个默认的Hello World" 代码。
按 F5,进入调试模式 。 不过控制台窗口会一闪而过。
添加一些代码:
我们加点代码,让程序在调入回车后再关闭。
-
在调用 WriteLine 方法后,立即加入以下代码:
Console.ReadLine();
-
检验一下你的代码是不是如下:
运行应用程序
-
点击工具栏上的 HelloWorld 以调试模式运行(或者按 F5)
-
在控制台窗口中查看程序。
关闭应用程序
-
敲ENTER 关闭黑窗口。
-
关闭VS的 Output 面板。
-
关掉 Visual Studio。