Behind the Scenes Look at C#: Operators, continued
(Page 1 of 5 )
We have discussed the various operators that C# defines. In this article we will take a closer look at the MSIL generated code. You will realize that the CLR has no knowledge of C# operators; it's just a special type of method with special names. After that we will discuss operator overloading.
So let's begin. In the next section I will show you the generated MSIL code, where we use the + operator to add two integers and two decimals in separate examples.
Operators Again, MSIL viewpoint
Looking at the MSIL generated code from an application that performs operations by using operators, you will wonder why the code is not the same between the built-in data types. Let's look at a simple example of an application that add two integers.
using System;
namespace Operators
{
class Class1
{
static void Main(string[] args)
{
int x = 4;
int y = 5;
Console.WriteLine(x + y);
}
}
}
Compile the application, then use the Ildasm.exe tool to load the MSIL code and navigate to the Main method code.
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 13 (0xd)
.maxstack 2
.locals init ([0] int32 x,
[1] int32 y)
IL_0000: ldc.i4.4
IL_0001: stloc.0
IL_0002: ldc.i4.5
IL_0003: stloc.1
IL_0004: ldloc.0
IL_0005: ldloc.1
IL_0006: add
IL_0007: call void [mscorlib]System.Console::WriteLine(int32)
IL_000c: ret
} // end of method Class1::Main
Where is the + operator? As we said before, the MSIL doesn't know anything about your C# operators; this example just clarifies that point. The + operator is expressed in the MSIL code as the add instruction (shown in the above code IL_0006: add). the add instruction takes two arguments from the stack and puts the result on the stack. it works with int, float, char, byte and the unsigned versions, but it doesn't work with the decimal data type. (Just as a side note, char is an integer value that represents the character, so the MSIL code knows only the integer value 97 of the character a, and so on).
Let's think about this for a minute. By using the add instruction, you can add two arguments and return the result without any process to the value or special manipulation -- but a data type such as decimal (or any user defined one like Customer, Order or Employee) needs a little more work. For example, the decimal data type needs some work to the precision operation, so the MSIL can't use the add instruction with these types.
Before we discuss the decimal data type with the + operator, I want to briefly discuss the MSIL above. The instruction ldc.i4.4 loads the constant value 4 on the stack. The instruction stloc.0 pops the value from the stack and stores it in the first local variable. The instruction ldc.i4.5 loads the constant value 5 on the stack, then the instruction stloc.1 pops the value from the stack and stores it in the second local variable. Both of the following instructions, ldloc.0 and ldloc.1, load the values of the local variables, number 0 and 1, on the stack, then the add instruction does its job and produces the sum.
Let's modify the application to work with the decimal data type.
using System;
namespace Operators
{
class Class1
{
static void Main(string[] args)
{
decimal x = 4;
decimal y = 5;
Console.WriteLine(x + y);
}
}
}
The MSIL code would be
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 27 (0x1b)
.maxstack 2
.locals init ([0] valuetype [mscorlib]System.Decimal x,
[1] valuetype [mscorlib]System.Decimal y)
IL_0000: ldc.i4.4
IL_0001: newobj instance void [mscorlib]System.Decimal::.ctor(int32)
IL_0006: stloc.0
IL_0007: ldc.i4.5
IL_0008: newobj instance void [mscorlib]System.Decimal::.ctor(int32)
IL_000d: stloc.1
IL_000e: ldloc.0
IL_000f: ldloc.1
IL_0010: call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Addition(valuetype [mscorlib]System.Decimal,
valuetype [mscorlib]System.Decimal)
IL_0015: call void [mscorlib]System.Console::WriteLine(valuetype [mscorlib]System.Decimal)
IL_001a: ret
} // end of method Class1::Main
This time the code is different and more complex. Do you see the add instruction? No, because as we said before, the decimal data type is complex and it can't use the add instruction. At the same time, the MSIL doesn't use operators, so the decimal data type defines a special type of method (op_Addition()) that performs the addition operation and returns the result. Inside this method is the code that will be called each time you write code that adds two decimal variables.
So with integers, the add instruction can do the job because it's just numbers, but with user defined types the MSIL needs a method to call to perform the addition operation. Note that the System.Int32 and System.Decimal both are built-in types. The add instruction can be used with the System.Int32 because it's just a simple number, but it can't be used with the System.Decimal because it doesn't know what to do; the developer of the type needs to write a method that will be called instead of the add instruction. The same issue exists with user defined types. We will look at an example soon and explain how it works.
Next: Operator overloading, first visit >>
More C# Articles
More By Michael Youssef