Part I
The C# Ecosystem
- : The C# Environment
- : Writing a First Program
- : Program and Code File Structure
Chapter 1
The C# Environment
Whats in This Chapter
- IL and the CLR
- JIT compiling
- Programs and assemblies
- The .NET Framework
Wrox.com Downloads for This Chapter
Please note that all the code examples for this chapter are available as a part of this chapters code download on the books website at www.wrox.com/go/csharp5programmersref on the Download Code tab.
A C# program cannot exist in isolation. You cant write C# programs without using other tools. You cant even run a compiled C# program without libraries that provide runtime support.
This chapter describes the tools that you need in the Windows environment to write, compile, and execute C# programs. Most of the time those tools work smoothly behind the scenes, so you dont need to be aware of their presence. Its still worth knowing what they are, however, so you know how all the pieces of the C# environment fit together.
Visual Studio
You can write a C# program in a text editor and then use a command-line interface to compile the program. (For example, see Working with the C# 2.0 Command Line Compiler at http://msdn.microsoft.com/library/ms379563.aspx
for more information.)
That approach is a lot of work, however, so most C# programmers use Visual Studio.
Visual Studio is a powerful integrated development environment ( IDE ) that includes code editors, form and window designers, and flexible debugging tools. Some versions also include testing, profiling, team programming, and other tools.
The Visual Studio code editors provide IntelliSense help, which displays prompts and descriptions of items you need to enter into the code. The code editors features such as IntelliSense make writing correct C# programs much easier than it is with a simple text editor.
If you havent already installed Visual Studio, you should probably do it now. It takes a while, so be sure you have a fast Internet connection.
To learn about and download one of the Visual Studio Express Editions, go to www.visualstudio.com/products/visual-studio-express-vs.
To learn about the other Visual Studio editions, go to www.microsoft.com/visualstudio/eng/products/compare.
While Visual Studio is downloading and installing, you can read further.
The most important tool integrated into Visual Studio is the compiler, which turns C# code into a compiled executable programwell, sort of.
The C# Compiler
The C# compiler doesnt actually compile code into a truly executable program. Instead it translates your C# code into an assembly-like language called Intermediate Language ( IL ).
Whats in a Name?
While under development, the intermediate language was called Microsoft Intermediate Language (MSIL). When .NET was released, the name was changed to IL.
The international standards organization Ecma created the Common Language Infrastructure (CLI) standard that defines a Common Intermediate Language (CIL).
To summarize the alphabet soup, MSIL is the old name for Microsofts intermediate language; IL is the current name; and CIL is the name for the non-Microsoft standard. There are some differences between IL and CIL but many .NET developers use MSIL, IL, and CIL interchangeably.
Programs and Assemblies
The C# compiler doesnt compile only programs; it can also compile other kinds of assemblies. An assembly is the smallest possible piece of compiled code. Assemblies include programs, code libraries, control libraries, and anything else you can compile. An executable program consists of one or more assemblies.
Consider the following C# code.
static void Main(string[] args){ foreach (string arg in args) Console.WriteLine(arg); Console.WriteLine("Press Enter to continue"); Console.ReadLine();}
The C# compiler translates this into the following IL code.
.method private hidebysig static void Main(string[] args) cil managed{ .entrypoint // Code size 51 (0x33) .maxstack 2 .locals init ([0] string arg, [1] string[] CS$6$0000, [2] int32 CS$7$0001, [3] bool CS$4$0002) IL_0000: nop IL_0001: nop IL_0002: ldarg.0 IL_0003: stloc.1 IL_0004: ldc.i4.0 IL_0005: stloc.2 IL_0006: br.s IL_0017 IL_0008: ldloc.1 IL_0009: ldloc.2 IL_000a: ldelem.ref IL_000b: stloc.0 IL_000c: ldloc.0 IL_000d: call void [mscorlib]System.Console::WriteLine(string) IL_0012: nop IL_0013: ldloc.2 IL_0014: ldc.i4.1 IL_0015: add IL_0016: stloc.2 IL_0017: ldloc.2 IL_0018: ldloc.1 IL_0019: ldlen IL_001a: conv.i4 IL_001b: clt IL_001d: stloc.3 IL_001e: ldloc.3 IL_001f: brtrue.s IL_0008 IL_0021: ldstr "Press Enter to continue" IL_0026: call void [mscorlib]System.Console::WriteLine(string) IL_002b: nop IL_002c: call string [mscorlib]System.Console::ReadLine() IL_0031: pop IL_0032: ret} // end of method Program::Main
Displaying IL
You can use the ildasm program to view a compiled programs IL code. (Ildasm is pronounced eye-ell-dazm so it rhymes with chasm. The name stands for IL disassembler.) For information about ildasm, see http://msdn.microsoft.com/library/f7dy01k1.aspx.
The IL code is fairly cryptic; although, if you look closely you can see the methods declaration and calls to Console.WriteLine
and Console.ReadLine
.
IL code looks a lot like assembly language but its not. Assembly language is a (barely) human-readable version of machine code that can run on a specific kind of computer. If the program were translated into assembly or machine code, it could run only on one kind of computer. That would make sharing the program on different computers difficult.
To make sharing programs on multiple computers easier, IL provides another layer between C# code and machine code. Its like a virtual assembly language that still needs to be compiled into executable machine code. You can copy the IL code onto different computers and then use another compiler to convert it into machine code at run time. In .NET, the Common Language Runtime ( CLR ) performs that compilation.
The CLR
CLR is a virtual machine component of the .NET Framework that translates IL into native machine code when you run a C# program. When you double-click a C# programs compiled executable program, the CLR translates the IL code into machine code that can be executed on the computer.
The CLR uses a just-in-time compiler ( JIT compiler ) to compile pieces of the IL code only when they are needed. When the program is loaded, the loader creates a stub for each method. Initially, that stub points to the methods IL code.
When the program invokes the method, the JIT compiler translates its IL code into machine code, makes the stub point to it, and then runs the machine code. If the program calls the method again later, its stub already points to the machine code, so the method doesnt need to be compiled again.
shows the process graphically.
Usually, the time needed to compile a method is small, so you dont notice the tiny bits of extra time used as each method is called for the first time. After a method is compiled, it runs a tiny bit faster when it is called later.
If a method is never called by the program, it is never compiled by the JIT compiler, so the compiler saves some time.
Next page