Keywords: C# Compilation | Command Line Compilation | CSC.exe | .NET Framework | Assembly
Abstract: This article provides a comprehensive guide on using CSC.exe compiler to compile and execute C# source files from command prompt. It covers compiler path location, basic compilation commands, multi-file compilation, assembly references, entry point requirements, and cross-platform compilation differences. The discussion extends to build tool selection and the role of command-line compilation in modern development workflows, offering developers a complete knowledge system from basics to advanced techniques.
Command Line Compilation Basics
CSC.exe is the C# compiler included in the .NET Framework and can be used directly from the command prompt window. This compiler can transform .cs source files into executable files or dynamic link libraries, depending on the specified target type. Using the /target:exe parameter generates .exe executable files, while /target:library produces DLL assemblies.
Compiler Path and Environment Configuration
CSC.exe resides in the .NET Framework installation directory, for example, version 3.5 is typically located at c:\windows\Microsoft.NET\Framework\v3.5\. To begin compilation, first open a command prompt window (accessible by typing cmd.exe in the Start menu), then use the cd command to navigate to the directory containing your source files.
Basic Compilation Commands
The fundamental command format for compiling a single C# source file is as follows:
c:\windows\Microsoft.NET\Framework\v3.5\bin\csc.exe /t:exe /out:MyApplication.exe MyApplication.csThis command must be entered on a single line, where /t:exe specifies the output type as executable, /out:MyApplication.exe defines the output filename, and MyApplication.cs is the input source file.
Multi-file Compilation and Assembly References
When a project contains multiple source files, you can list all required .cs files on the same command line. If external assembly references are needed, use the /r:AssemblyName.dll parameter. For example:
csc.exe /t:exe /out:MyApp.exe Main.cs Helper.cs /r:System.Data.dllThis flexibility enables command-line compilation to handle complex project structures effectively.
Entry Point Requirement
When generating executable files, a static Main() method must be defined in one of your classes to serve as the program entry point. This is a mandatory requirement for .NET application startup, and the compiler will verify this condition, reporting errors if absent.
Executing Compilation Results
After successful compilation, simply type the generated executable filename (e.g., MyApplication) in the command prompt and press Enter to run the program. This approach operates independently of any integrated development environment, providing the foundation for automated deployment and continuous integration.
Cross-platform Compilation Differences
On macOS systems, the C# compiler usage is similar, but the compiler name is simplified to csc. Compilation command example:
$ csc /target:exe /out:MyApplication.exe MyApplication.csExecution requires the Mono runtime:
$ mono MyApplication.exeThis demonstrates the cross-platform nature of the .NET ecosystem.
Advanced Compilation Options
CSC.exe supports extensive compilation options, including resource embedding, icon setting, assembly signing, and more. These options enable command-line compilation to achieve the same build capabilities as Visual Studio. Developers can explore various parameters in depth through MSDN documentation.
Build Tool Integration
While command-line compilation is valuable, real-world projects typically integrate with build tools. Tools like NAnt, MSBuild, and FinalBuilder provide complete build environments that better manage complex dependencies and build processes. MSBuild, as Microsoft's official build tool, supports multiple project types and manages build configurations through .*PROJ files.
Development Environment Optimization
If Visual Studio is installed, you can use the "Visual Studio command prompt," which automatically sets all necessary environment variables and paths, simplifying command-line compilation configuration. This integrated approach enhances development efficiency while maintaining the flexibility of command-line compilation.
Compilation Model Comparison
Unlike Java's javac compiler, which produces .class files later packaged together, CSC.exe directly compiles .cs files into complete assemblies. This design difference reflects the distinct architectural characteristics of the two language runtimes. C# assemblies can execute directly, while Java class files require the Java Virtual Machine to run.
Practical Application Recommendations
For simple projects, direct compilation using CSC.exe is an efficient choice. However, as project complexity increases, migration to MSBuild-based build systems is recommended. In modern development practices, command-line compilation is commonly used in continuous integration pipelines, automated testing, and deployment scripts, becoming an essential component of DevOps workflows.