Dictionary.Item[] Property in C#

The Dictionary.Item[] property in C# provides a convenient way to get or set values in a Dictionary using indexer syntax. This property allows you to access dictionary values directly using square bracket notation with the key, similar to array indexing.

Syntax

Following is the syntax for the Dictionary.Item[] property −

public TValue this[TKey key] { get; set; }

Parameters

  • key − The key of the element to get or set.

Return Value

The property returns the value associated with the specified key. If the key is not found when getting, it throws a KeyNotFoundException. When setting, it either updates the existing value or adds a new key-value pair if the key doesn't exist.

Using Item[] Property for Getting Values

The Item[] property can be used to retrieve values from a Dictionary by specifying the key −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        Dictionary<string, string> dict = new Dictionary<string, string>();
        dict.Add("One", "Chris");
        dict.Add("Two", "Steve");
        dict.Add("Three", "Messi");
        dict.Add("Four", "Ryan");
        dict.Add("Five", "Nathan");
        
        Console.WriteLine("Count of elements = " + dict.Count);
        Console.WriteLine("\nKey/value pairs...");
        foreach(KeyValuePair<string, string> res in dict) {
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
        }
        
        Console.WriteLine("Value for key Three = " + dict["Three"]);
        
        if (dict.ContainsValue("Angelina"))
            Console.WriteLine("\nValue found!");
        else
            Console.WriteLine("\nValue isn't in the dictionary!");
    }
}

The output of the above code is −

Count of elements = 5

Key/value pairs...
Key = One, Value = Chris
Key = Two, Value = Steve
Key = Three, Value = Messi
Key = Four, Value = Ryan
Key = Five, Value = Nathan
Value for key Three = Messi

Value isn't in the dictionary!

Using Item[] Property for Setting Values

The Item[] property can also be used to update existing values or add new key-value pairs −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        Dictionary<string, string> dict = new Dictionary<string, string>();
        dict.Add("One", "Chris");
        dict.Add("Two", "Steve");
        dict.Add("Three", "Messi");
        
        Console.WriteLine("Original value for key Three = " + dict["Three"]);
        
        // Update existing value
        dict["Three"] = "Ronaldo";
        Console.WriteLine("Updated value for key Three = " + dict["Three"]);
        
        // Add new key-value pair
        dict["Six"] = "Katie";
        Console.WriteLine("Added new key Six with value = " + dict["Six"]);
        Console.WriteLine("Total count after addition = " + dict.Count);
    }
}

The output of the above code is −

Original value for key Three = Messi
Updated value for key Three = Ronaldo
Added new key Six with value = Katie
Total count after addition = 4

Handling Non-Existent Keys

When accessing a key that doesn't exist, the Item[] property throws a KeyNotFoundException. It's recommended to check if the key exists using ContainsKey() method −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        Dictionary<string, int> scores = new Dictionary<string, int>();
        scores["Alice"] = 95;
        scores["Bob"] = 87;
        scores["Charlie"] = 92;
        
        string searchKey = "David";
        
        if (scores.ContainsKey(searchKey)) {
            Console.WriteLine(searchKey + " scored: " + scores[searchKey]);
        } else {
            Console.WriteLine(searchKey + " not found in the dictionary");
        }
        
        // Safe way to get value or default
        Console.WriteLine("Alice's score: " + scores["Alice"]);
    }
}

The output of the above code is −

David not found in the dictionary
Alice's score: 95

Conclusion

The Dictionary.Item[] property provides an intuitive way to access and modify dictionary values using square bracket notation. It can both retrieve existing values and add or update key-value pairs, making it essential for efficient dictionary operations in C#.

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

513 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements