Gets or sets the value in HybridDictionary with specified key in C#

The HybridDictionary class in C# provides an indexer property that allows you to get or set values using a specified key. The HybridDictionary is part of the System.Collections.Specialized namespace and combines the benefits of both ListDictionary and Hashtable for optimal performance with small and large collections.

Syntax

Following is the syntax for getting or setting values in HybridDictionary using the indexer −

// Getting a value
object value = hybridDict[key];

// Setting a value
hybridDict[key] = value;

Parameters

  • key − The key of the element to get or set. Can be any object type.

  • value − The value to assign to the specified key. Can be any object type.

Return Value

The indexer returns the value associated with the specified key. If the key does not exist, it returns null.

Getting Values from HybridDictionary

Example

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      HybridDictionary myDict = new HybridDictionary();
      myDict.Add("1", "Tablet");
      myDict.Add("2", "Desktop");
      myDict.Add("3", "Speakers");
      myDict.Add("4", "Laptop");
      myDict.Add("5", "Notebook");
      myDict.Add("6", "Ultrabook");
      myDict.Add("7", "HDD");
      myDict.Add("8", "SSD");
      
      Console.WriteLine("Value at key 5 = " + myDict["5"]);
      Console.WriteLine("Value at key 3 = " + myDict["3"]);
      Console.WriteLine("Value at key 8 = " + myDict["8"]);
   }
}

The output of the above code is −

Value at key 5 = Notebook
Value at key 3 = Speakers
Value at key 8 = SSD

Setting Values in HybridDictionary

Example

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      HybridDictionary myDict = new HybridDictionary();
      myDict.Add("1", "Tablet");
      myDict.Add("2", "Desktop");
      myDict.Add("3", "Speakers");
      myDict.Add("4", "Laptop");
      myDict.Add("5", "Notebook");
      
      Console.WriteLine("Original value at key 5 = " + myDict["5"]);
      
      // Update existing key
      myDict["5"] = "Smart Watches";
      Console.WriteLine("Updated value at key 5 = " + myDict["5"]);
      
      // Add new key-value pair using indexer
      myDict["6"] = "Headphones";
      Console.WriteLine("New value at key 6 = " + myDict["6"]);
   }
}

The output of the above code is −

Original value at key 5 = Notebook
Updated value at key 5 = Smart Watches
New value at key 6 = Headphones

Handling Non-Existent Keys

Example

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      HybridDictionary myDict = new HybridDictionary();
      myDict.Add("1", "Tablet");
      myDict.Add("2", "Desktop");
      
      // Accessing existing key
      Console.WriteLine("Value at key 1 = " + myDict["1"]);
      
      // Accessing non-existent key
      object value = myDict["99"];
      if (value == null) {
         Console.WriteLine("Key 99 does not exist");
      }
      
      // Setting value for non-existent key creates new entry
      myDict["99"] = "New Device";
      Console.WriteLine("Value at key 99 after setting = " + myDict["99"]);
   }
}

The output of the above code is −

Value at key 1 = Tablet
Key 99 does not exist
Value at key 99 after setting = New Device

Key Features

  • The indexer can both get and set values using the same syntax.

  • Setting a value for a non-existent key automatically creates a new key-value pair.

  • Getting a value for a non-existent key returns null instead of throwing an exception.

  • Keys and values can be of any object type.

Conclusion

The HybridDictionary indexer provides a convenient way to access and modify values using keys. It automatically handles both existing and non-existent keys, making it flexible for dynamic collections where keys may be added or updated frequently.

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

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements