Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate a IDictionary/Dictionary Object

I have code where this is declared:

 public IDictionary<string, OPTIONS> dict_Options = new Dictionary<string, OPTIONS>();

 public class OPTIONS
 {
        public string subjectId = string.Empty;
        public string varNumber = string.Empty;
        public string varName = string.Empty;
 }

What's the easiest way to iterate over all the varNames in my dictionary object? Is there like a foreach?

like image 327
cdub Avatar asked Dec 21 '22 12:12

cdub


2 Answers

        foreach (var item in dict_Options)
        {
            string varName = item.Value.varName;
        }

This iterates through all the KeyValuePair<T, T> in your dictionary

like image 124
Bas Avatar answered Dec 24 '22 01:12

Bas


Just to add an alternative to the great answers already posted, you could do:

dict_Options.Keys.ToList().ForEach(m => SomeFunc(m.Value.varName));
like image 29
Jamie Dixon Avatar answered Dec 24 '22 01:12

Jamie Dixon