接受给出的第一个完整答案。
感谢所有做出回应的人!
<小时/>我是 C# 和 Visual Studio 的新手,所以如果这是一个基本问题,请原谅我。
if (!exceptions.Contains(accountNodeID))
{
dummyTotals.Add(accountNodeID, accountNodeTotal);
}
- 异常(exception)是
List<string>
- accountNodeID 是
string
- dummyTotals 是
Dictionary<string, int>
如果异常(exception)仅包含一个元素,包含会识别accountNodeID何时位于异常(exception)中。
如果异常(exception)由2个以上元素组成,Contains无法识别accountNodeID何时位于异常(exception)中。
Visual Studio 调试器的屏幕截图 [因此不允许我在此处发布,因为我必须创建一个新帐户:( ]:
- accountNodeID is in exceptions, but is added to dummyTotals anyway .
- but that doesn't happen when exceptions contains only one element .
谁能告诉我以下内容吗?
- 为什么Contains无法识别accountNodeID是异常(exception)中的几个元素之一 - 我的代码当然看起来像所有示例据我所知,我能找到
- 当accountNodeID位于异常(exception)中的多个元素中时,如何防止将accountNodeID添加到dummyTotals
非常高兴根据需要提供其他相关信息!非常感谢!!!
<小时/>编辑
感谢您已经提供的信息。我现在正在看。作为记录,以下是创建列表的方式(迭代 XML 文件中的节点并使用Add)。
List<string> dummyList = new List<string>();
while (exceptionsIterator.MoveNext())
{
exceptions.Add(exceptionsIterator.Current.Value);
}
我认为这是添加列表元素而不是将值连接到字符串上。
<小时/>编辑
请参阅我对答案的最终评论,以了解此问题的最终解决方案。
最佳答案
您的调试屏幕截图显示异常仅包含一个元素:“541 139 434”
所以问题一定出在异常的填充方式上。
当您添加到List<string>
时,您可以通过调用 .Add(string)
来完成此操作。
我怀疑有这样的代码:
List<string> exceptions = new List<string>(String.Join(" ", accountNodeIds));
这不是填充列表的方法。相反:
List<string> exceptions = new List<string>(accountNodeIds);
或
exceptions.AddRange(accountNodeIds);
或
foreach (string accountNodeId in accountNodeIds) {
exceptions.Add(accountNodeId);
}
关于c# - 当列表包含 x,y,z 时,为什么 list.Contains(x) 找不到 x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22074514/