.class public abstract auto ansi sealed beforefieldinit ClassLibrary1.Hello extends [mscorlib]System.Object
{
.method public hidebysig static string SayHello(string name) cil managed
{
// Code size 12 (0xc) .maxstack 8 IL_0000: ldstr "Hello, " IL_0005: ldarg.0 IL_0006: call string [mscorlib]System.String::Concat(string, string) IL_000b: ret
} // end of method Hello::SayHello }
// end of class ClassLibrary1.Hello
On the other hand, this is the code generated from the Main function (which is also the same from VB.NET/C#):
.class public abstract auto ansi sealed beforefieldinit
ConsoleApplication1.Program extends [mscorlib]System.Object
{
.method private hidebysig static void Main() cil managed
{
.entrypoint .maxstack 8 IL_0000: ldstr "Mohammad Elsheimy" IL_0005: call string [ClassLibrary1]ClassLibrary1.Hello::SayHello(string) IL_000a: call
void [mscorlib]System.Console::WriteLine(string) IL_000f: ret }
// end of method Program::Main }
// end of class ConsoleApplication1.Program
You can use the ILDasm.exe tool to get the CIL code of an assembly. This tool is located in Program Files\Microsoft SDKs\Windows\<version>\bin.
Here comes a question, is there CIL developers? Could we write the CIL directly and build it into .NET assembly? Why we can't find much (if not any) CIL developers? You can extract the answer from the CIL code itself. As you see, CIL is not so friendly and its statements are not so clear. Plus, if we could use common languages to generate the CIL, we we'd like to program in CIL directly? So it's better to leave the CIL for the compiler.
Now, let's see the other form of .NET interoperation, Unmanaged Code Interoperability.
Summary
So, the secret of Managed Code Interoperation falls in the Common Intermediate Language or CIL. When you compile your code, the compiler converts your C#/VB.NET (or any other .NET language) to CIL instructions and saves them in the assembly, and that's the secret. The linking between .NET assemblies of different languages relies on the fact that the linking is actually done between CILs of the assemblies. The assembly code doesn't (usually) have any clue about the language used to develop it. In the runtime, the compiler reads those instructions and converts them to machine instructions and execute them.
Next, we'll talk about the other form of .NET Interoperation, it's the interoperation with unmanaged code.