views:

147

answers:

3

And why doesn't it shows the real type (eg: List<string> instead of List`1)?
Where does this strange (to me) notation comes from?

+5  A: 

The CLR name for generic List<T> with one type parameter is

System.Collections.Generic.List`1

List<T> is the C# style of naming that type.

The notation is a convention defined to give you the ability to overload generic types by parameter count. If you declare a class named List with two type parameters (e.g. List<T,U>) the compiler would be able to choose the right one. It'll be named:

System.Collections.Generic.List`2

and a non-generic List class will be named:

System.Collections.Generic.List

Note that the CLR does not require generic types to be named like that. It's just a convention for the compilers to be able to pick the right type.

Mehrdad Afshari
I thought the CLI didn't erase generic types? If not, where does "String" when "List<String>" becomes List`1?
Chris Conway
`List<T>` becomes List\`1. It's the "open generic type definition." The closed definition of `List<string>` will become List\`1[System.String].
Mehrdad Afshari
+4  A: 

It doesn't show List<string> because that's C# dependent syntax. A List in .Net source code could be declared as List<string>, List(Of String) or any of the other 60 or so languages/syntaxes that can compile down to MSIL.

So what you're seeing is the more generic MSIL syntax.

The `1 means that it is a List that has one generic parameter.

womp
+2  A: 

The `1 is MSIL syntax for a generic type - List<T> is C#-specific. More MSIL examples:

List<T> == System.Collections.Generic.List`1

List<string> == System.Collections.Generic.List`1[System.String]

Dictionary<string, int> == System.Collections.Generic.Dictionary`2[System.String,System.Int32]

thecoop
I think you missed the point. \`1 doesn't define a generic class for the CLR. At the IL level, a generic class can be arbitrarily named. It's a method to resolve ambiguities between overloaded generic types for higher level languages. It declares the number of type parameters for the type so that you can overload types by the number of generic parameters. Your example is wrong. `Dictionary<string, int>` is Dictionary\`2[System.String,System.Int32], not Dictionary\`1, since it has **two** type parameters.
Mehrdad Afshari
whoops, typo. Fixed.
thecoop