如何检查Hashtable值是否都为0?

mar*_*zzz 2 c# hashtable cycle

这是我的代码:

Hashtable actualValues = new Hashtable();
actualValues.Add("Field1", Int32.Parse(field1.Value));
actualValues.Add("Field2", Int32.Parse(field2.Value));
actualValues.Add("Field3", Int32.Parse(field3.Value));

bool isAllZero = true;
foreach (int actualValue in actualValues)
{
    if (actualValue > 1)
        isAllZero = false;
}
if (isAllZero)
{

}
Run Code Online (Sandbox Code Playgroud)

但我System.InvalidCastException: Specified cast is not valid.在6号线接近这个例外foreach.

我哪里错了?

vc *_* 74 5

假设你可以使用Linq

bool isAllZero = Hashtable.Cast<DictionaryEntry>().All(pair => (int)pair.Value == 0);
Run Code Online (Sandbox Code Playgroud)

如果你HashTable用a 替换Dictionary<string, int>它,它就变成了

bool isAllZero = dictionary.All(pair => pair.Value == 0);
Run Code Online (Sandbox Code Playgroud)