CHAPTER 1 THE PHILOSOPHY OF .NET
// Wait for user to press the Enter key before shutting down.
Console.ReadLine();
}
}
// The C# calculator.
class Calc
{
public int Add(int x, int y)
{ return x + y; }
}
}
After you compile this code file using the C# compiler (csc.exe), you end up with a single-file *.exe
assembly that contains a manifest, CIL instructions, and metadata describing each aspect of the Calc
and Program classes.
Note Chapter 2 examines the details of compiling code using the C# compiler, as well as the use of graphical
IDEs such as Microsoft Visual Studio (among others).
For example, if you were to open this assembly using ildasm.exe (examined a little later in this
chapter), you would find that the Add() method is represented using CIL such as the following:
.method public hidebysig instance int32 Add(int32 x,
int32 y) cil managed
{
// Code size 9 (0x9)
.maxstack 2
.locals init (int32 V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: add
IL_0004: stloc.0
IL_0005: br.s IL_0007
IL_0007: ldloc.0
IL_0008: ret
} // end of method Calc::Add
Don’t worry if you are unable to make heads or tails of the resulting CIL for this method—Chapter
17 will describe the basics of the CIL programming language. The point to concentrate on is that the C#
compiler emits CIL, not platform-specific instructions.
Now, recall that this is true of all .NET-aware compilers. To illustrate, assume you created this same
application using Visual Basic, rather than C#.
' Calc.vb
Imports System
Namespace CalculatorExample
11