我刚刚遇到一个奇怪的错误:
private bool GetBoolValue()
{
//Do some logic and return true or false
}
Run Code Online (Sandbox Code Playgroud)
然后,在另一种方法中,这样的事情:
int? x = GetBoolValue() ? 10 : null;
Run Code Online (Sandbox Code Playgroud)
很简单,如果方法返回true,则为Nullable intx 赋值10 .否则,将null赋给nullable int.但是,编译器抱怨:
错误1无法确定条件表达式的类型,因为
int和之间没有隐式转换<null>.
我疯了吗?
抓我的头.以下陈述有什么问题?
var EncFunc = (encrypt ? Encryption.Encrypt : Encryption.Decrypt);
Run Code Online (Sandbox Code Playgroud)
encrypt是bool,两个函数Encryption.Encrypt和Encryption.Decrypt具有相同的类型Func<string, string>,但它告诉我:
CS0173无法确定条件表达式的类型,因为"方法组"和"方法组"之间没有隐式转换
我已经经历过这个和这个,但无法理解为什么编译器无法确定这两个函数的类型.
NB我知道这可以通过显式转换来修复.我对理解"为什么"部分更感兴趣.