CIL (Common Intermediate Language) is a low-level language used by Microsoft .NET Framework and Mono.
Questions tagged [il]
477 questions
150
votes
1 answer
Why does the C# compiler translate this != comparison as if it were a > comparison?
I have by pure chance discovered that the C# compiler turns this method:
static bool IsNotNull(object obj)
{
return obj != null;
}
…into this CIL:
.method private hidebysig static bool IsNotNull(object obj) cil managed
{
ldarg.0 // obj
…

stakx - no longer contributing
- 83,039
- 20
- 168
- 268
146
votes
3 answers
Performance of static methods vs instance methods
My question is relating to the performance characteristics of static methods vs instance methods and their scalability. Assume for this scenario that all class definitions are in a single assembly and that multiple discrete pointer types are…

Bernie White
- 4,780
- 4
- 23
- 21
105
votes
3 answers
Why does my application spend 24% of its life doing a null check?
I've got a performance critical binary decision tree, and I'd like to focus this question on a single line of code. The code for the binary tree iterator is below with the results from running performance analysis against it.
public…

Will Calderwood
- 4,393
- 3
- 39
- 64
49
votes
1 answer
Stackoverflow doing boxing in C#
I have these two chunks of code in C#:
First
class Program
{
static Stack S = new Stack();
static int Foo(int n) {
if (n == 0)
return 0;
S.Push(0);
S.Push(1);
...
S.Push(999);
…

lcastillov
- 2,163
- 1
- 11
- 17
41
votes
4 answers
Can C# 'is' operator suffer under release mode optimization on .NET 4?
Below is a simple test fixture. It succeeds in Debug builds and fails in Release builds (VS2010, .NET4 solution, x64):
[TestFixture]
public sealed class Test
{
[Test]
public void TestChecker()
{
var checker = new Checker();
…

Cumbayah
- 4,415
- 1
- 25
- 32
37
votes
9 answers
Why does the compiler let me cast a null to a specific type in C#?
Consider this code:
var str = (string)null;
When write the code this is my IL code:
IL_0001: ldnull
And IL has any Cast operator but:
var test = (string) new Object();
The IL code is:
IL_0008: castclass [mscorlib]System.String
So Casting null…
user1968030
35
votes
4 answers
IL Instructions not exposed by C#
What IL instructions are not exposed by C#?
I'm referring to instructions like sizeof and cpblk - there's no class or command that executes these instructions (sizeof in C# is computed at compile time, not at runtime AFAIK).
Others?
EDIT: The reason…

YellPika
- 2,872
- 2
- 28
- 31
34
votes
2 answers
A tool for easy IL code inspection
Sometimes I would like to quickly see the IL representation of my code snippets in C#, to understand what exactly happens to various code statements under the hood, like it's done here for example.
I know there are ildasm, Reflector, ILSpy, dotPeek…

doblak
- 3,036
- 1
- 27
- 22
33
votes
3 answers
Viewing the IL code generated from a compiled expression
Is it possible to view the IL code generated when you call Compile() on an Expression tree? Consider this very simple example:
class Program
{
public int Value { get; set; }
static void Main(string[] args)
{
var param =…
user47589
31
votes
5 answers
IL level code debugger
Is there any IL level debugger in form of a VS plugin or standalone application?
Visual studio’s debugger is great, but it allows you to debug on either HLL code level or assembly language, you can’t debug IL.
It seems that in some situations it…

axk
- 5,316
- 12
- 58
- 96
31
votes
3 answers
.NET functions disassembled
When disassembling .NET functions, I notice that they all start with a similair pattern.
What does this initial code do?
This code appear before the actual code for what the function is supposed to do.
Is it some sort of parameter count…

Roger Johansson
- 22,764
- 18
- 97
- 193
30
votes
1 answer
Why does generated IL code start with a Nop?
I was trawling through some of the IL of one of my assemblies (via ILDasm) and I noticed that all of my methods begin with a nop instruction.
Does anyone know why that is?

YellPika
- 2,872
- 2
- 28
- 31
30
votes
7 answers
Determine whether .NET assemblies were built from the same source
Does anyone know of a way to compare two .NET assemblies to determine whether they were built from the "same" source files?
I am aware that there are some differencing utilities available, such as the plugin for Reflector, but I am not interested in…

Clayton
- 301
- 3
- 4
29
votes
7 answers
Interlocked.CompareExchange using GreaterThan or LessThan instead of equality
The System.Threading.Interlocked object allows for Addition (subtraction) and comparison as an atomic operation. It seems that a CompareExchange that just doesn't do equality but also GreaterThan/LessThan as an atomic comparison would be quite…

makerofthings7
- 60,103
- 53
- 215
- 448
28
votes
5 answers
Why is the C# compiler emitting a callvirt instruction for a GetType() method call?
I am curious to know why this is happening. Please read the code example below and the corresponding IL that was emitted in comments below each section:
using System;
class Program
{
static void Main()
{
Object o = new Object();
…

Andrew Hare
- 344,730
- 71
- 640
- 635