现在的位置: 首页 > 综合 > 正文

Cast on a “float” sequence throws InvalidCastException

2012年09月18日 ⁄ 综合 ⁄ 共 395字 ⁄ 字号 评论关闭

原因 :The Cast method only supports reference and boxing conversions. Use Select instead:

就是说cast<>()方法只支持装箱和拆箱操作,其他强制类型转换不支持。

比如:你要完成以下代码,会抛出异常的:

static void Main(string[] args)
{
Hashtable st
= new Hashtable();
st.Add(
"a",12);
st.Add(
"b",13);
st.Add(
"c", 14);
double sum = st.Values.Cast<float>().Sum();
}

而这样是可以的:

Hashtable st = new Hashtable();
st.Add(
"a",12f);
st.Add(
"b",13f);
st.Add(
"c", 14f);
double sum = st.Values.Cast<float>().Sum();

抱歉!评论已关闭.