从字典中获取值

本文关键字:获取 字典 | 更新日期: 2023-09-27 18:06:49

我将采用轮询算法。我所做的一切都必须是动态的和随机的。当我想第二次去检查我是否有并且value大于0在检查点它说"给定的键不存在于字典中"。我该如何解决这个问题?

private int GetNextNodeToProcesssRandom()
{
    int NextNodeIndex = -1;
    if (NextNodeToProcess >= this.waytosave.Count)
    {
        NextNodeToProcess = 0;
    }
    for (int i = NextNodeToProcess; i < this.waytosave.Count; i++)
    {
        if (this.waytosave[i] > 0)//the problem appears here when the cod goes for the second time.
        {
            NextNodeIndex = i;
            break;
        }
    }
    NextNodeToProcess++;
    return NextNodeIndex;
} 

从字典中获取值

在某种程度上,您的确切目标是什么并不清楚。但是如果你想循环遍历一个Dict,你可以使用:

foreach(KeyValuePair<TKey, TValue> entry in MyDict)
{
    // do something with entry.Value or entry.Key
}

现在,如果你想在你的程序中添加一个检查,你可以尝试:

    if (MyDict.ContainsKey(this.waytosave[i]))
///continue execution...

看这里可能会有帮助