Compile C# programs from the command line

1. Create a new helloworld.cs file and add code.

using System;
namespace HelloWorldApplication
{
   class HelloWorld
   {
      static void Main(string[] args)
      {
         /* 第一个 C# 程序*/
         Console.WriteLine("Hello World");
         Console.ReadKey();
      }
   }
}

2. Open the command prompt tool and navigate to the directory where the file is saved.

3. Type csc helloworld.cs and press enter to compile the code.

If you are prompted that the csc command cannot be recognized, you need to configure environment variables

Find the "Computer" icon on the desktop, right-click, and click "Properties" - "Advanced System Settings" - "Environment Variables" - "System Variables" in the pop-up menu, find the variable Path, and add a path; C: \Windows\Microsoft.NET\Framework\v2.0.50727\ (note that multiple paths are separated by a semicolon (;), and other versions of Windows can be appended).

Note: The Windows folder may be hidden

4. If there are no errors in the code, the command prompt goes to the next line and generates the helloworld.exe executable.

5. Next, type helloworld to execute the program.

6. You will see "Hello World" printed on the screen.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325746149&siteId=291194637