Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetType().ToString() Returns Unexpected Characters

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.

like image 568
JWD Avatar asked Sep 03 '13 16:09

JWD


4 Answers

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.

like image 145
Reed Copsey Avatar answered Nov 14 '22 21:11

Reed Copsey


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]
like image 32
Jon Skeet Avatar answered Nov 14 '22 20:11

Jon Skeet


`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

like image 36
Zbigniew Avatar answered Nov 14 '22 20:11

Zbigniew


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.

like image 1
SLaks Avatar answered Nov 14 '22 21:11

SLaks