Compile and execute C# source files using the command prompt
This guide will show you how to use the C# compiler (csc.exe) from the command prompt to compile and execute C# source files (.cs).
Compile C# source files
To compile a C# source file, open a command prompt by typing "cmd.exe" in the Start menu. Navigate to the directory containing the source code.
Run the following command:
<code>c:\windows\Microsoft.NET\Framework\v3.5\bin\csc.exe /t:exe /out:MyApplication.exe MyApplication.cs</code>
Replace "MyApplication.cs" with the name of your source file and "MyApplication.exe" with your desired executable output name.
If your source code spans multiple modules, include them on the same command line. To reference an external assembly, use "/r:
Execute executable file
Once compilation is complete, you can execute the resulting executable file by typing the following in the command prompt:
<code>MyApplication</code>
Visual Studio Command Prompt
If Visual Studio is installed, open Visual Studio Command Prompt from the Start menu to set the necessary environment variables.
Build Tools
While command line compilation is useful, consider using a build tool like NAnt or MSBuild to create a more comprehensive build environment.
macOS
On macOS, the syntax is similar, but the compiler is named "csc":
<code>$ csc /target:exe /out:MyApplication.exe MyApplication.cs</code>
To execute the executable file:
<code>$ mono MyApplication.exe</code>
The above is the detailed content of How to Compile and Run C# Code Using the Command Prompt?. For more information, please follow other related articles on the PHP Chinese website!