Get a collection of values in the StringDictionary in C#

The StringDictionary class in C# provides a collection that maps strings to strings, where keys are case-insensitive. To retrieve values from a StringDictionary, you can access the Values property or iterate through key-value pairs to extract the values.

StringDictionary automatically converts all keys to lowercase, making it useful for scenarios where case-insensitive string lookups are required.

Syntax

Following is the syntax to access values in a StringDictionary −

StringDictionary stringDict = new StringDictionary();
ICollection values = stringDict.Values;
string value = stringDict[key];

Using Values Property

The Values property returns an ICollection containing all values in the StringDictionary −

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringDictionary strDict = new StringDictionary();
      strDict.Add("A", "Apple");
      strDict.Add("B", "Banana");
      strDict.Add("C", "Cherry");
      strDict.Add("D", "Date");

      Console.WriteLine("All values in StringDictionary:");
      ICollection values = strDict.Values;
      foreach(string value in values) {
         Console.WriteLine("Value: " + value);
      }

      Console.WriteLine("\nTotal values count: " + values.Count);
   }
}

The output of the above code is −

All values in StringDictionary:
Value: Apple
Value: Banana
Value: Cherry
Value: Date

Total values count: 4

Using Keys to Access Values

You can iterate through keys and access their corresponding values −

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringDictionary strDict = new StringDictionary();
      strDict.Add("S", "Home Appliances");
      strDict.Add("T", "Smart Wearable Tech");
      strDict.Add("U", "Electronics");
      strDict.Add("V", "Toys");
      strDict.Add("W", "Books");
      strDict.Add("X", "Accessories");

      Console.WriteLine("StringDictionary elements...");
      foreach(DictionaryEntry d in strDict) {
         Console.WriteLine(d.Key + " " + d.Value);
      }

      Console.WriteLine("\nCollection of keys and values...");
      String[] keyArr = new String[strDict.Count];
      strDict.Keys.CopyTo(keyArr, 0);
      for (int i = 0; i < strDict.Count; i++) {
         Console.WriteLine("Key " + (i+1) + " = " + keyArr[i] + ", Value " + (i+1) + " = " + strDict[keyArr[i]]);
      }

      Console.WriteLine("\nDoes StringDictionary has key B? " + strDict.ContainsKey("B"));
   }
}

The output of the above code is −

StringDictionary elements...
x Accessories
s Home Appliances
t Smart Wearable Tech
u Electronics
v Toys
w Books

Collection of keys and values...
Key 1 = x, Value 1 = Accessories
Key 2 = s, Value 2 = Home Appliances
Key 3 = t, Value 3 = Smart Wearable Tech
Key 4 = u, Value 4 = Electronics
Key 5 = v, Value 5 = Toys
Key 6 = w, Value 6 = Books

Does StringDictionary has key B? False

Case-Insensitive Key Behavior

StringDictionary converts all keys to lowercase automatically. This demonstrates the case-insensitive nature −

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringDictionary strDict = new StringDictionary();
      strDict.Add("A", "John");
      strDict.Add("B", "Andy");
      strDict.Add("C", "Tim");
      strDict.Add("D", "Ryan");
      strDict.Add("E", "Kevin");

      Console.WriteLine("StringDictionary elements...");
      foreach(DictionaryEntry d in strDict) {
         Console.WriteLine(d.Key + " " + d.Value);
      }

      Console.WriteLine("\nAccessing values by different case keys:");
      Console.WriteLine("strDict["A"]: " + strDict["A"]);
      Console.WriteLine("strDict["a"]: " + strDict["a"]);
      Console.WriteLine("strDict["B"]: " + strDict["B"]);
      Console.WriteLine("strDict["b"]: " + strDict["b"]);

      Console.WriteLine("\nDoes StringDictionary has key 'B'? " + strDict.ContainsKey("B"));
      Console.WriteLine("Does StringDictionary has key 'b'? " + strDict.ContainsKey("b"));
   }
}

The output of the above code is −

StringDictionary elements...
a John
b Andy
c Tim
d Ryan
e Kevin

Accessing values by different case keys:
strDict["A"]: John
strDict["a"]: John
strDict["B"]: Andy
strDict["b"]: Andy

Does StringDictionary has key 'B'? True
Does StringDictionary has key 'b'? True

Conclusion

The StringDictionary class provides case-insensitive string-to-string mapping with the Values property returning all stored values. Keys are automatically converted to lowercase, making both uppercase and lowercase key access equivalent for retrieving values.

Updated on: 2026-03-17T07:04:36+05:30

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements