Reader Level:

Compiling and Debugging

By Praveen Moosad on Nov 08, 2009
In this this article I will explain you about compilation and debugging in .NET.

This article has been excerpted from book "Visual C# Programmer's Guide"

We generally write programs in higher-level languages such as C# or Java because they are easier to work with and are more readable than assembly language or machine code. Therefore, using a higher-level language makes a programmer's task easier and less prone to error. However, because the computer understands only binary code, we must somehow convert the source code files of a higher-level language into code the machine can understand. This process is called code compilation, and the program that converts the source code into machine code is called a compiler.

C# runs on top of the .NET Framework, taking advantage of features such as automatic garbage collection and base class libraries. Hence the .NET Framework adds another level of abstraction between the machine and the C# code. All languages on the .NET platform, including C#, compile into Microsoft Intermediate Language (MSIL) code instead of machine code. MSIL code is just like assembly language code, but it's highly optimized and diversified to support a variety of programming languages. The MSIL is considered an intermediate language because at runtime the MSIL code is compiled into machine-level code by the Just-In-Time (JIT) compiler. Unlike the one-step approach of the C/C++ compiler, which converts source code directly into machine level code, C# takes a two-step approach. First, the C# compiler compiles the source code into MSIL code; then, at runtime, MSIL code gets converted into machine-level code by the JIT compilers provided by the .NET Framework.

The .NET Framework provides three JIT compilers: StandardJIT, PreJIT, and EconoJIT. StandardJIT, the default JIT compiler, is generally used to compile all your normal .NET applications. PreJIT, as the name suggests, compiles all the MSIL code into machine-level code at once during installation. This compiler can be very useful when you want to use an application frequently. The EconoJIT compiler is used with handheld systems, personal digital assistants, and other systems that have limited resources. It is optimized to save memory but takes a bit longer to execute code.

Using the C# Command-Line Compiler

The C# language can be compiled into MSIL using either the Visual Studio .NET (VS.NET) integrated development environment (IDE) or the command-line compiler. Even users of the VS.NET IDE may find the various aspects of command-line compiling interesting because it helps them understand the C# compiler and its capabilities.

The C# compiler is contained in the file csc.exe and gets installed when you install the .NET Framework. Even though it is a command-line compiler, you can use it to compile all kinds of C# applications, for example, console applications or Windows forms applications (Visual C#). The C# compiler is generally located within the <drive>:\<windows>\Microsoft.NET\Framework\<version>\ folder, so on a typical Windows 2000 machine the path to the compiler is found in c:\WinNT\Microsoft.NET\Framework\v1.0.3328\.

To run the C# compiler, you must first open the DOS console containing the Command Prompt/MSDOS Prompt. Then you navigate to the directory containing the source code files, and finally you compile the source files. Try calling the csc compiler from the command prompt, as follows: c:\>csc. If you get a error such as 'csc' is not recognized as an internal or external command, operable program or batch file, you must set the environment variables for the compiler in one of two ways. If you have installed VS.NET, search for and run the file vsvars32.bat; if you only have the .NET Framework, then search for and run the file corvars.bat.


Hope this article would have helped you in understanding compilation and debugging in .NET. See my other articles on the website on .NET and C#.

Praveen Moosad
Praveen Moosad

C# Corner Chief Editor and 3-times Microsoft MVP. I hold Masters degree in Computer Science and Applications and Bachelor’s degree in Mathematics. I am responsible for content publishing, product development, and m... Read more