我有以下代码:
public int Number(int x)
{
return x > 0 ? x : throw new Exception();
}目标非常简单,使用运算符“?”我想检查一些值,如果它满足条件,返回那个值,如果不满足,就抛出一些错误。但是VS Intellisense说:表达式术语抛出无效;我是否被迫使用其他运算符?
附注:我猜它和return throw new Exception();是一样的,但还是想确认一下。
发布于 2017-02-14 00:45:27
用什么?表达式两端必须返回相同的类型,这在您的情况下不是真的。替换为if(x > 0) return x throw new Exception();
https://stackoverflow.com/questions/42209135
复制相似问题