假设我想写一些类似的东西:
static string Foo(dynamic d, string key)
{
var dict = d as IDictionary<string, string>;
if (dict?.TryGetValue(key, out string result) ?? false)
{
return result;
}
return null;
}
它给出了编译错误:Local variable 'result' migth not be initialized before accessing
我想知道为什么 c# 编译器在这种情况下不确定控制流。
我们都知道,大多数时候 c# 编译器在看到常量值时表现得“聪明”。