我对 .Net 相当陌生,并且正在研究基础对象数组如何处理不同的类型。我倾向于在开关中使用 GetType().ToString() 组合来集中代码中的事件处理,我遇到了以下返回值。我在这里和其他地方(包括 C# 规范)寻找答案,但找不到直接解决的地方:
代码:
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());
}
}
}
}
输出:
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]
从我的角度来看,第 5 - 8 行的“`1”部分是出乎意料的。
a)有人可以解释这里发生了什么吗?b) 有没有办法预测这些(或其他)字符何时会包含在输出中?我可以看到它们在使用构造类型时存在,而在使用内在类型时则不存在。
顺便说一句,这是我对stackoverflow的第一个问题。在过去的几年里,我在这里找到了大量有价值的信息。这是我第一次找不到答案。