【发布时间】:2019-06-24 22:55:31
【问题描述】:
为什么会这样:
System.Collections.Stack s = new Stack();
s.Push(97);
char c = (char) s.Pop(); //throws InvalidCastException
抛出一个错误,但是这个:
char c = (char) 97; //c = 'a'
工作正常吗?
我特别困惑,因为s.Pop().GetType() 返回System.Int32,所以这真的不重要......
这里发生了什么?我错过了什么,还是我必须解决它?
【问题讨论】:
-
s.Push((char)97);? -
我想知道为什么你甚至使用来自
System.Collections命名空间的东西而不是System.Collections.Generic -
@RubensFarias 是的,我可以在这个例子中做到这一点,但不是在我正在编程的东西的上下文中。这大大简化了。
标签: c# type-conversion