Loop through System.Collections.Generic.Dictionary through for statement

I have a quick question. Is there a way to simplify the loop through System.Collections.Generic.Dictionary through a for statement in C #?

Thanks in advance.

+6
generics c # loops
source share
9 answers

Not in a smart way, no. You can use the Linq ElementAt :

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

... but I really don't see the point. Just use the usual foreach approach. If for some reason you need to track the index during an iteration, you can do this from the side:

 int index = 0; foreach (var item in dictionary) { Console.WriteLine(string.Format("[{0}] - {1}", index, item.Value)); // increment the index index++; } 
+7
source share

You can use foreach:

 Dictionary<string,string> dictionary = new Dictionary<string,string>(); // ... foreach (KeyValuePair<string,string> kv in dictionary) { string key = kv.Key; string value = kv.Value; } 
+11
source share

There are several ways. Quoting via keys:

 foreach(var key in myDictionary.Keys) 

cyclic values:

 foreach(var value in myDic.Values) 

loop through pairs:

 foreach(KeyValuePair<K, V> p in myDic) { var key = p.Key; var value = p.Value } 
+2
source share

No, for is mainly for collections with indexes. However, you can foreach easily.

0
source share

If you are using Net 3.5 or later, you can use LINQ and preecate to define a specific value or values. Dictionaries do not necessarily keep their KeyValue pairs in order (either by input order or by key order).

0
source share

Philip got it for foreach , although I usually simplify it:

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

It is not possible to skip this set of key-value pairs using a for loop because they are not stored in order. You can scroll through keys or values ​​as collections.

0
source share

It can be done, but it's a little silly IMO

  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(); } 

The only thing that comes out of this is the (rather useless) index, and if that is the actual goal, you like something best:

  int currentIndex = 0; foreach (KeyValuePair<string, string> keyValuePair in dictionary) { //do something usefull currentIndex++; } 
0
source share

You can navigate through the keys

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

or values

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

Or as Philip shows

-one
source share

If it should not be “for” -loop, perhaps using the Dictionary.GetEnumerator Method with the “while” -loop is an option - it's pretty easy, but IMHO:

 var enumerator = d.GetEnumerator(); while (enumerator.MoveNext()) { var pair = enumerator.Current; b += pair.Value; } enumerator.Dispose(); 

code snippet from C # Dictionary GetEnumerator

-one
source share

All Articles