在我的桌面C#应用程序中,我从字典开始.我希望能够检查这个字典中的密钥.如果字典有这个键,我想把它传递给一个方法.如果字典没有这个键,我想创建一个空白列表,然后将其传递给它.我怎样才能做到这一点?
我收到错误"给定密钥不在字典中".我可以添加一个默认值,因此它永远不会为null吗?
// myDic was declared as a Dictionary<string, List<string>
// Here is how I call someFunction
string text = SomeFunction(stringValue1, stringValue2, myDic[field1.field2]);
// SomeFunction looks like this
string SomeFunction (string string1, string string2, List<string> ra)
{
// method
return stringResult;
}
Run Code Online (Sandbox Code Playgroud)