小编Bri*_*ord的帖子

即使键存在,TryGetValue 返回 Null?

我正在梳理一些字典对象以获得一个值,但它们都返回 null,即使当我检查它们是否包含该键时,具有该键的对象也会返回 true。我还可以使用 foreach 遍历每个值以获取基于该键的值......我很迷失

foreach (var item in treeView.SelectedItems)
{
    string reportName = item.Header.ToString();
    string reportPath = "";

    reportsAvail.TryGetValue(reportName, out reportPath);
    reports.TryGetValue(reportName, out reportPath);
    additionalReports.TryGetValue(reportName, out reportPath);

    bool test; 

    test = reportsAvail.ContainsKey(reportName);
    test = reports.ContainsKey(reportName);
    test = additionalReports.ContainsKey(reportName);

    foreach (var y in reportsAvail)
    {
        if (y.Key.ToString() == reportName)
        {
            textBlock1.Text = y.Value;
            reportPath = y.Value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是它曾经起作用......我不确定是什么阻止了它

c# wpf null dictionary trygetvalue

1
推荐指数
1
解决办法
6986
查看次数

标签 统计

c# ×1

dictionary ×1

null ×1

trygetvalue ×1

wpf ×1