这可能是一个基本问题,但为什么在将值类型列表传递到泛型方法时,我无法将泛型类型转换回原始类型?为什么我不能将泛型类型转换为值类型
IList<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };
Inverse<int>(list);
public void Inverse<T>(IList<T> list)
{
for (i = 0; i <= list.Count/2; i++)
{
int a = list[i] as Int16; //=> does not work
int b = (int)list[i]; //=> does not work either
}
}
我wouldnt推荐转换。在这种情况下,它将在对象上调用ToString,然后解析它。而你不知道T可以藏起来。 – Euphoric 2011-03-05 22:41:00