我有这样的代码:错误消息:“无法转换类型'字符串?'以“串”“
//Return null if the extension doesn't have the value, returns the value if it does.
private T? getValue<T>(IEnumerable<Extension> extension, string attributeName)
{
IEnumerable<Extension> ext = extension.Where(e => e.attributeName == attributeName);
if (ext.Count() > 0)
{
return (T)ext.First().Attribute;
}
return null;
}
我打电话是这样的:
//This works:
retVal.byteValue= getValueFromExtension<byte>(u, "ByteToGet") ?? 0;
//This doesn't work:
getValueFromExtension<string>(u, "Text") ?? "";
我得到的编译错误:”错误消息:“无法将类型“字符串? “以“串”“
我怎样才能有效地做好代码的理念之上,而无需创建一个新的方法?
我觉得我检查,如果它是空与??运营商,因此,如果字符串为空,它将总是被设置为一个空字符串,它是如何处理字节和整数,为什么不是字符串?
上面的字节值,是字节类型,不是字节?
这段代码编译过的?我很困惑'T?'方法的返回类型 – sll
什么是'getValueFromExtension'? – Oded
我不明白为什么你的方法甚至编译没有'struct'约束。 – CodesInChaos