Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is !0 a type in Microsoft Intermediate Language (MSIL)?

Tags:

c#

.net

cil

In many MSIL listings, I have observed the following:

System.Nullable`1<!0> etc ... 

or

class !0 etc ... 

What's the meaning of !0 in these circumstances?

like image 777
Dragno Avatar asked Jul 22 '15 09:07

Dragno


People also ask

What is the use of Microsoft Intermediate Language?

Microsoft Intermediate Language (MSIL) is a CPU-independent set of instructions that can be efficiently converted to the native code. During the runtime the Common Language Runtime (CLR)'s Just In Time (JIT) compiler converts the Microsoft Intermediate Language (MSIL) code into native code to the Operating System.

Why have Microsoft selected the MSIL?

The MSIL is platform independent and consequently, it can be executed on any of the Common Language Infrastructure supported environments such as the Windows . NET runtime. The MSIL is converted into a particular computer environment specific machine code by the JIT compiler.

What is the meaning of MSIL?

Microsoft Intermediate Language, a programming language that has been standardized later as the Common Intermediate Language.

What is Microsoft intermediate language in .NET framework?

The Microsoft Intermediate Language (MSIL) also goes by the name Common Intermediate Language (CIL). It is typically a set of platform-independent instructions created from source code by a language-specific compiler. It is produced by a variety of compilers (C#, VB,. NET, and so on).


2 Answers

This is quirk of the decompiler you use to look at a .NET assembly. It is the behavior of ildasm.exe, other ones like Reflector or ILSpy get this right. The Microsoft programmer who wrote it took a shortcut, he generates a string from the IL that just displays the type argument the way it is encoded, without writing the extra code to lookup the type argument name in the metadata.

You need to read !n as the n-th type argument of the generic type. Where !0 means "first type argument", !1 means "second type argument", etcetera. For Nullable<>, you know that '!0` means 'T' from the MSDN article.

You may also encounter something like !!T. Two exclamation marks indicate a type argument for a generic method. This time, ildasm.exe does look up the type argument name instead of using !!0. Why the programmer took the shortcut on generic types but not on generic methods is hard to reverse-engineer. Ildasm is a pretty quirky program and is written in a C++ coding style that's very different from other C++ code in .NET. Not as disciplined, non-zero odds that this was an intern's assignment :)

The `1 suffix on "Nullable" is a normal encoding for generic type names, it indicates the generic type has one type argument. In other words, for Nullable<> you'll never see !1 being used.

So simply read !0 as "T". Or use a better decompiler.

like image 51
Hans Passant Avatar answered Oct 21 '22 01:10

Hans Passant


That is a generic type parameter.

They are positional.

Decompile some generic code to see how they are used (compare IL vs C#).

like image 23
leppie Avatar answered Oct 21 '22 01:10

leppie