我正在尝试TryGetValue
像往常一样在字典上使用,如下面的代码:
Response.Context.Skills[MAIN_SKILL].UserDefined.TryGetValue("action", out var actionObj)
我的问题是字典本身可能是 null。我可以简单地使用“?”。在 UserDefined 之前,但随后我收到错误:
"cannot implicitly convert type 'bool?' to 'bool'"
我可以处理这种情况的最佳方法是什么?UserDefined
在使用 TryGetValue 之前是否必须检查是否为空?因为如果我不得不使用Response.Context.Skills[MAIN_SKILL].UserDefined
两次我的代码可能看起来有点乱:
if (watsonResponse.Context.Skills[MAIN_SKILL].UserDefined != null &&
watsonResponse.Context.Skills[MAIN_SKILL].UserDefined.TryGetValue("action", out var actionObj))
{
var actionName = (string)actionObj;
}