Questions tagged [cil]

Common Intermediate Language is the object-oriented assembly language used by the .NET Framework, .NET Core, and Mono. .NET languages compile to CIL, which is assembled into an object code that has a bytecode-style format.

Common Intermediate Language (CIL, pronounced either "sil" or "kil") (formerly called Microsoft Intermediate Language or MSIL, and sometimes informally called IL) is the lowest-level human-readable programming language defined by the Common Language Infrastructure (CLI) specification and is used by the .NET Framework, .NET Core, and Mono.

Languages which target a CLI-compatible runtime environment compile to CIL, which is assembled into an object code that has a bytecode-style format. CIL is an object-oriented assembly language, and is stack-based. Its bytecode is translated into native code or executed by a virtual machine.

For CIL the C Intermediate Language (a subset of C), use .

1064 questions
54
votes
2 answers

Why is it necessary to call :this() on a struct to use automatic properties in c#?

If I define a struct in C# using automatic properties like this: public struct Address { public Address(string line1, string line2, string city, string state, string zip) { Line1 = line1; Line2 = line2; City = city; …
NerdFury
  • 18,876
  • 5
  • 38
  • 41
43
votes
3 answers

Generate tail call opcode

Out of curiosity I was trying to generate a tail call opcode using C#. Fibinacci is an easy one, so my c# example looks like this: private static void Main(string[] args) { Console.WriteLine(Fib(int.MaxValue, 0)); } public…
devshorts
  • 8,572
  • 4
  • 50
  • 73
38
votes
1 answer

What's the difference between sizeof(T) and Unsafe.SizeOf()?

First of all, a small disclaimer before the actual question: I know there are a lot of closed/duplicate questions regarding the difference between the sizeof operator and the Marshal.SizeOf method, and I do understand the difference between the…
Sergio0694
  • 4,447
  • 3
  • 31
  • 58
37
votes
4 answers

What's the point of MethodImplOptions.InternalCall?

Many methods in the BCL are marked with the [MethodImpl(MethodImplOptions.InternalCall)] attribute. This indicates that the "method is implemented within the common language runtime itself". What was the point of designing the framework in this…
Ani
  • 111,048
  • 26
  • 262
  • 307
31
votes
3 answers

C# compiler doesn’t optimize unnecessary casts

A few days back, while writing an answer for this question here on overflow I got a bit surprised by the C# compiler, who wasn’t doing what I expected it to do. Look at the following to code snippets: First: object[] array = new object[1]; for (int…
Steven
  • 166,672
  • 24
  • 332
  • 435
30
votes
7 answers

Writing a Compiler for .net - IL or Bytecode?

I'm currently diving into the inner workings of .net, which means IL. As an exercise, I want to build a brainf..k compiler for .net (yes, they already exist, but as said it's for learning purposes). For the moment I'm just writing some text files…
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
30
votes
2 answers

Compiler generated incorrect code for anonymous methods [MS BUG FIXED]

See the following code: public abstract class Base { public virtual void Foo() where T : class { Console.WriteLine("base"); } } public class Derived : Base { public override void Foo() { …
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
28
votes
7 answers

How do i prevent my code from being stolen?

What happens exactly when I launch a .NET exe? I know that C# is compiled to IL code and I think the generated exe file just a launcher that starts the runtime and passes the IL code to it. But how? And how complex process is it? IL code is embedded…
Calmarius
  • 18,570
  • 18
  • 110
  • 157
28
votes
1 answer

Why do I have to do ldarg.0 before calling a field in MSIL?

I want to call a function, with as parameters a string and an Int32. The string is just a literal, the Int32 should be a field. So I thought it should be something like: .method public hidebysig instance string TestVoid() cil managed { .maxstack…
Jan Jongboom
  • 26,598
  • 9
  • 83
  • 120
27
votes
6 answers

Creating method dynamically, and executing it

Background: I want to define few static methods in C# , and generate IL code as byte array, from one of these methods, selected at runtime (on client), and send the byte array over network to another machine (server) where it should be executed…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
27
votes
2 answers

Implicit conversion to System.Double with a nullable struct via compiler generated locals: why is this failing?

Given the following, why does the InvalidCastException get thrown? I can't see why it should be outside of a bug (this is in x86; x64 crashes with a 0xC0000005 in clrjit.dll). class Program { static void Main(string[] args) { …
codekaizen
  • 26,990
  • 7
  • 84
  • 140
27
votes
1 answer

Closure allocations in C#

I've installed the Clr Heap Allocation Analyzer extension and in a project I see something that I quite don't understand, I've got a method with a signature public Task ExecuteAsync(string sql, dynamic param = null, IDbTransaction transaction =…
evilpilaf
  • 1,991
  • 2
  • 21
  • 38
27
votes
5 answers

How to insert CIL code to C#

Is it possible to insert IL code to C# method?
Duiner
  • 273
  • 1
  • 3
  • 4
26
votes
6 answers

A .net disassembler/decompiler

I am looking for a disassembler or better, a decompiler for .net. The situation is that the source code for an assembly written by one of my predecessors is lost and I'd like to take a look to see what it's doing. I know that ildasm comes with the…
Dana
  • 32,083
  • 17
  • 62
  • 73
26
votes
1 answer

Why does 'unbox.any' not provide a helpful exception text the way 'castclass' does?

To illustrate my question, consider these trivial examples (C#): object reference = new StringBuilder(); object box = 42; object unset = null; // CASE ONE: bad reference conversions (CIL instrcution 0x74 'castclass') try { string s =…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
1
2
3
70 71