编译 & 执行 C# 程序
using System;
using System.Collections.Generic;
using System.Text;
namespace MyProgram1
{
class program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}
如果您使用 Visual Studio.Net 编译和执行 C# 程序,请按下面的步骤进行:
- 启动 Visual Studio。
- 在菜单栏上,选择 File -> New -> Project。
- 从模板中选择 Visual C#,然后选择 Windows。
- 选择 Console Application。
- 为您的项目制定一个名称,然后点击 OK 按钮。
- 新项目会出现在解决方案资源管理器(Solution Explorer)中。
- 在代码编辑器(Code Editor)中编写代码。
- 点击Run 按钮或者按下 F5 键来运行程序。会出现一个命令提示符窗口(Command Prompt window),显示 Hello World。
您也可以使用命令行代替 Visual Studio IDE 来编译 C# 程序:
- 打开一个文本编辑器,添加上面提到的代码。
- 保存文件为 helloworld.cs。
- 打开命令提示符工具,定位到文件所保存的目录。
- 键入 csc helloworld.cs 并按下 enter 键来编译代码。
- 如果代码没有错误,命令提示符会进入下一行,并生成 helloworld.exe 可执行文件。
- 接下来,键入 helloworld 来执行程序。
- 你将看到 “Hello World” 打印在屏幕上。
注意:
1. 若提示无法识别 csc 命令,需配置环境变量(Window10)
找到桌面上的”计算机”图标,右键单击,并在弹出的菜单中点击 “属性” –”高级系统设置”–”环境变量”–”系统变量”,找到变量 Path,
添加一个路径 ;C:\Windows\Microsoft.NET\Framework\v2.0.50727\(注意,多个路径使用分号(;)隔开,Windows其他版本追加在后面即可)。
2.C#和Java的不同点之一:
Java的类名必须和文件名相同,C#则不用。