Home >Backend Development >C++ >How Can I Compile and Run C# Code from the Command Line?

How Can I Compile and Run C# Code from the Command Line?

Susan Sarandon
Susan SarandonOriginal
2025-01-15 22:26:48436browse

How Can I Compile and Run C# Code from the Command Line?

C# command line compilation and execution

This article explains how to compile and run C# (.cs) files using the command line. This requires the C# compiler (csc.exe) included with the .NET Framework.

Compilation and execution under Windows system:

  1. In the command prompt, navigate to the directory containing the C# files.
  2. Execute the following command to compile:
<code>c:\windows\Microsoft.NET\Framework\v3.5\bin\csc.exe /t:exe /out:MyApplication.exe MyApplication.cs</code>

Replace "MyApplication" with your desired executable name. The /t:exe option specifies the output as an executable file, and the /out option specifies the output file name.

  1. Once the compilation is complete, navigate to the directory containing the .exe file in the command prompt.
  2. Execute the following command to run the executable file:
<code>MyApplication</code>

You can also start "Visual Studio Command Prompt" in the "Visual Studio Tools" menu of the start menu, which will automatically configure the environment for command line compilation.

Compilation and execution under macOS system:

  1. Install the Mono framework, which provides a C# compiler.
  2. Open a terminal window and navigate to the directory containing the C# files.
  3. Execute the following command to compile:
<code>$ csc /target:exe /out:MyApplication.exe MyApplication.cs</code>
  1. Execute the following command to run the executable file:
<code>$ mono MyApplication.exe</code>

Tip: For more complex projects, it is recommended to use a build tool such as NAnt or MSBuild to create a complete build environment rather than just relying on a basic compiler.

The above is the detailed content of How Can I Compile and Run C# Code from the Command Line?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn