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.
The
Type.ToString
method uses`1
as a marker for generic types (the1
refers to "one" generic parameter). This:is the actual type name for
in C# syntax. Note that each language can have it's own syntax. For example, this same type, in VB, is:
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.