CHAPTER 1 THE PHILOSOPHY OF .NET
' A VB "Module" is a class that contains only
' static members.
Module Program
Sub Main()
Dim c As New Calc
Dim ans As Integer = c.Add(10, 84)
Console.WriteLine("10 + 84 is {0}.", ans)
Console.ReadLine()
End Sub
End Module
Class Calc
Public Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function
End Class
End Namespace
If you examine the CIL for the Add() method, you find similar instructions (slightly tweaked by the
Visual Basic compiler, vbc.exe).
.method public instance int32 Add(int32 x,
int32 y) cil managed
{
// Code size 8 (0x8)
.maxstack 2
.locals init (int32 V_0)
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: add.ovf
IL_0003: stloc.0
IL_0004: br.s IL_0006
IL_0006: ldloc.0
IL_0007: ret
} // end of method Calc::Add
Source Code The Calc.cs and Calc.vb code files are included under the Chapter 1 subdirectory.
Benefits of CIL
At this point, you might be wondering exactly what is gained by compiling source code into CIL rather
than directly to a specific instruction set. One benefit is language integration. As you have already seen,
each .NET-aware compiler produces nearly identical CIL instructions. Therefore, all languages are able
to interact within a well-defined binary arena.
Furthermore, given that CIL is platform-agnostic, the .NET Framework itself is platform-agnostic,
providing the same benefits Java developers have grown accustomed to (e.g., a single code base running
12