Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过for语句循环通过System.Collections.Generic.Dictionary_C#_Generics_Loops - Fatal编程技术网

C# 通过for语句循环通过System.Collections.Generic.Dictionary

C# 通过for语句循环通过System.Collections.Generic.Dictionary,c#,generics,loops,C#,Generics,Loops,我有一个简短的问题。有没有办法通过C#中的语句的轻松循环通过System.Collections.Generic.Dictionary 提前感谢。您可以使用foreach: Dictionary<string,string> dictionary = new Dictionary<string,string>(); // ... foreach (KeyValuePair<string,string> kv in dictionary) { st

我有一个简短的问题。有没有办法通过C#中的语句的轻松循环通过
System.Collections.Generic.Dictionary


提前感谢。

您可以使用foreach:

Dictionary<string,string> dictionary = new Dictionary<string,string>();

// ...

foreach (KeyValuePair<string,string> kv in dictionary) 
{
    string key = kv.Key;
    string value = kv.Value;
}
Dictionary Dictionary=newdictionary();
// ...
foreach(字典中的KeyValuePair kv)
{
串键=千伏键;
串值=千伏值;
}

否,的
主要用于具有索引的集合。

但是,您可以轻松地对每个键进行
foreach

您可以在键上循环

 for(int i = 0; i < myDictionary.Keys.Length; ++i)
      myDictionary.Keys[i] ...
for(int i=0;i
还是价值观

 for(int i = 0; i < myDictionary.Values.Length; ++i)
      myDictionary.Values[i] ...
for(int i=0;i

或者,如Philippe所示,如果您使用的是NET3.5或更高版本,则可以使用LINQ和predecate来定位一个或多个特定值。 字典不一定按顺序(按条目顺序或键顺序)存储键值对。

有几种方法。 循环通过键:

foreach(var key in myDictionary.Keys)
通过值循环:

foreach(var value in myDic.Values)
成对循环:

foreach(KeyValuePair<K, V> p in myDic)
{  
     var key = p.Key;
     var value = p.Value
}
foreach(myDic中的KeyValuePair p)
{  
var-key=p.key;
var值=p.值
}

Philippe为foreach获得了它,尽管我通常将其简化为:

foreach (var pair in dictionary) 
{
    var key = pair.Key;
    var value = pair.Value;
}

无法使用
for
循环遍历这个键值对集合,因为它们没有按顺序存储。但是,您可以将键或值作为集合进行循环。

这不是一种合理的方式,不可以。您可以使用Linq扩展名
元素at

for (int i = 0; i < dictionary.Keys.Count; i++)
{
    Console.WriteLine(dictionary.ElementAt(i).Value);                
}

这是可以做到的,但在我看来有点傻

        Dictionary<string,string> dictionary = new Dictionary<string, string>();
        Dictionary<string, string>.Enumerator enumerator = dictionary.GetEnumerator();
        for (int i = 0; i < attributeValues.Count;i++ )
        {
            KeyValuePair<string, string> current = enumerator.Current;
            //do something usefull
            enumerator.MoveNext();
        }
Dictionary Dictionary=newdictionary();
Dictionary.Enumerator Enumerator=Dictionary.GetEnumerator();
for(int i=0;i
通过这一点获得的唯一好处是一个(相当无用的)索引,如果这是实际目标,那么您最好使用以下内容:

        int currentIndex = 0;
        foreach (KeyValuePair<string, string> keyValuePair in dictionary)
        {
            //do something usefull
            currentIndex++;
        }
int currentIndex=0;
foreach(字典中的KeyValuePair KeyValuePair)
{
//做一些有用的事
currentIndex++;
}

如果它不必是“for”循环,那么可以选择使用带有“while”循环的-这也很简单,IMHO:


来自

的代码片段您想做什么?你在寻找一个特定的值或键值对吗?我想你是指foreach而不是for?事实上,它已经被修复了,不会编译
不能将带[]的索引应用于“System.Collections.Generic.DictionaryKeyCollection”类型的表达式。
他在寻找的是:Count而不是Length,仅适用于“正常”arrays@Oxymoron:即使使用
Count
,尝试按索引访问
Keys
集合中的元素(
Keys[i]
)仍将破坏生成(这是我在注释中提到的错误)。使用“var”一般来说,这是非常糟糕的做法。@nyrguds这是一个非常全面和客观的陈述。这也是不正确的-在使用匿名类型的任何情况下,
var
关键字都是绝对必要的。此外,在本例中,询问者没有在字典中指定泛型类型,我发现(在现实世界中)有时需要更改类型。我使用的
var
使字典的确切类型变得无关紧要,并且无论泛型类型如何,它都是有效的代码。有很多地方我不会让一个
var
通过代码审查,但在一个示例代码片段中,这很好。为什么要使用
foreach
来完成这项工作?另外,您没有处理枚举器。代码段来自,不是我的代码。根据该网站的说法,使用枚举器比使用“foreach”性能更好,这就是为什么我认为值得一提的原因枚举器不管是不是你的代码。这不是好代码,您要么修复它,要么不使用它。处理枚举器很重要;如果要显式枚举,则需要这样做。至于说它比一个
foreach
,这正是
foreach
要做的
foreach
只是语法上的糖分(除了
foreach
与您的代码不同,它做得正确之外)。A
foreach
不会变慢;它也在做同样的事情。
        int currentIndex = 0;
        foreach (KeyValuePair<string, string> keyValuePair in dictionary)
        {
            //do something usefull
            currentIndex++;
        }
var enumerator = d.GetEnumerator();
while (enumerator.MoveNext())
{
var pair = enumerator.Current;
b += pair.Value;
}
enumerator.Dispose();