GetType().ToString() Returns Unexpected Characters

3.5k views Asked by At

I'm fairly new to .Net and was looking at how a base object array would handle different types. I tend to use the GetType().ToString() combination in a switch to centralize my event handling in code and I encountered the following return values. I've looked for an answer here and elsewhere (including the C# spec) but can't find where this is directly addressed:

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Objectify
{
    class Program
    {
        static void Main(string[] args)
        {
            object[] arrObjects = {"", 0, 0.0M, 'a', new Stack<string>(), new Queue<string>(), new Stack<int>(), new Queue<int>()};

            for (int i = 0; i < arrObjects.Length; i++ )
            {
                Console.WriteLine(arrObjects[i].GetType().ToString());
            }           
        }
    }
}

Output:

System.String
System.Int32
System.Decimal
System.Char
System.Collections.Generic.Stack`1[System.String]
System.Collections.Generic.Queue`1[System.String]
System.Collections.Generic.Stack`1[System.Int32]
System.Collections.Generic.Queue`1[System.Int32]

The "`1" part of lines 5 - 8 are unexpected from my perspective.

a) Can someone explain what is going on here? b) Is there some way to anticipate when these (or other) characters will be included in the output? I can see that they are present when a Constructed Type is used, and not so when an intrinsic type is used.

BTW, this is my first question to stackoverflow. I've found tons of valuable information here over the past couple of years. This is the first time I haven't been able to find an answer.

6

There are 6 answers

0
Reed Copsey On BEST ANSWER

The Type.ToString method uses `1 as a marker for generic types (the 1 refers to "one" generic parameter). This:

 System.Collections.Generic.Stack`1[System.String] 

is the actual type name for

 System.Collections.Generic.Stack<System.String>

in C# syntax. Note that each language can have it's own syntax. For example, this same type, in VB, is:

 System.Collections.Generic.Stack(Of System.String)

The key difference is that Type.ToString is part of the CLR, and not tied to a specific language (like C#), so the syntax used for displaying generic types differs.

0
Jon Skeet On

Sure - the types are generic. The `1 part indicates the number of generic type parameters (always 1 in your case), and the [System.String] or [System.Int32] part indicates the type argument used for that type parameter.

So for example, if you created a Dictionary<int, string> you'd get:

System.Collections.Generic.Dictionary`2[System.Int32,System.String]
0
Zbigniew On

`1 is a number of generic type parameters.

System.Collections.Generic.Queue`1[System.Int32]

Means a Queue with 1 type parameter which is Int32

0
SLaks On

This is the CLR's notation for generic types.
The `1 means that the type has one generic parameter.

This is necessary because generic types can be overloaded – there are 8 types with the name System.Action.

0
Dave Bish On

The CLR sometimes uses special/reserved characters behind the scenes, to ensure you cant create a class/declaration with the same name as an auto-generated one.

What you're seeing here is what a generated generic type really looks like, behind the scenes

2
ProgramFOX On

Can someone explain what is going on here?

The `1[System.String] means that the type has one type parameters, which is System.String in this case.