Home >Backend Development >C++ >How Do I Compile and Run a C# Program from the Command Line?
Command-Line C# Compilation and Execution
This guide demonstrates compiling and running C# code directly from the command line, a valuable skill when an IDE isn't readily available.
The process utilizes the C# compiler (csc.exe
), located within the .NET Framework directory (e.g., c:windowsMicrosoft.NETFrameworkv3.5
).
Windows:
<code class="language-batch">c:\windows\Microsoft.NET\Framework\v3.5\bin\csc.exe /t:exe /out:MyApplication.exe MyApplication.cs ...</code>
Replace MyApplication.exe
and MyApplication.cs
with your desired output and source file names respectively. Use the /r
flag to include additional modules or assembly references. Remember that your code must contain a Main()
method as an entry point.
MyApplication
) and pressing Enter.For advanced options, consult the MSDN documentation on the command-line compiler. Alternatively, Visual Studio users can leverage the Visual Studio command prompt, which pre-configures necessary environment variables.
macOS:
The macOS process is similar, using csc
for compilation and mono
for execution:
<code class="language-bash">$ csc /target:exe /out:MyApplication.exe MyApplication.cs ... $ mono MyApplication.exe</code>
Beyond Basic Compilation:
While command-line compilation offers flexibility, robust build processes often benefit from dedicated build tools like NAnt or MSBuild. These tools provide a more comprehensive and manageable build environment for larger projects.
The above is the detailed content of How Do I Compile and Run a C# Program from the Command Line?. For more information, please follow other related articles on the PHP Chinese website!