What is the Item property of SortedList class in C#?

The Item property of the SortedList class in C# gets and sets the value associated with a specific key in the SortedList. It allows you to access and modify elements using the indexer syntax [key], making SortedList operations intuitive and similar to working with arrays or dictionaries.

The Item property can also be used to add new elements directly. If the key does not exist, it creates a new key-value pair. If the key already exists, it overwrites the existing value with the new one.

Syntax

Following is the syntax for using the Item property −

// Get value
object value = sortedList[key];

// Set value
sortedList[key] = value;

Parameters

  • key − The key whose value to get or set.

Return Value

Returns the value associated with the specified key. If the key does not exist when getting, it throws a KeyNotFoundException.

Using Item Property for Access and Modification

Example

using System;
using System.Collections;

namespace Demo {
    class Program {
        static void Main(string[] args) {
            SortedList s = new SortedList();
            s.Add("S001", "Jack");
            s.Add("S002", "Henry");
            
            // Using Item property to add new element
            s["S003"] = "Brad";
            
            // Using Item property to modify existing element
            s["S001"] = "Jackson";
            
            // Get a collection of the keys
            ICollection key = s.Keys;
            foreach (string k in key) {
                Console.WriteLine(k + ": " + s[k]);
            }
        }
    }
}

The output of the above code is −

S001: Jackson
S002: Henry
S003: Brad

Using Item Property for Safe Access

Example

using System;
using System.Collections;

namespace Demo {
    class Program {
        static void Main(string[] args) {
            SortedList studentGrades = new SortedList();
            studentGrades["Alice"] = 95;
            studentGrades["Bob"] = 87;
            studentGrades["Charlie"] = 92;
            
            // Safe access using ContainsKey
            string studentName = "Alice";
            if (studentGrades.ContainsKey(studentName)) {
                Console.WriteLine(studentName + " scored: " + studentGrades[studentName]);
            }
            
            // Adding or updating grades
            studentGrades["David"] = 89;  // New entry
            studentGrades["Alice"] = 98;  // Update existing
            
            Console.WriteLine("Updated grades:");
            foreach (DictionaryEntry entry in studentGrades) {
                Console.WriteLine(entry.Key + ": " + entry.Value);
            }
        }
    }
}

The output of the above code is −

Alice scored: 95
Updated grades:
Alice: 98
Bob: 87
Charlie: 92
David: 89

Key Rules

  • If the key exists, the Item property retrieves or updates the associated value.

  • If the key does not exist when setting, it adds a new key-value pair to the SortedList.

  • If the key does not exist when getting, it throws a KeyNotFoundException.

  • Use ContainsKey() method to safely check if a key exists before accessing it.

Conclusion

The Item property of SortedList provides a convenient indexer syntax for accessing, modifying, and adding elements using keys. It maintains the sorted order automatically and offers both retrieval and assignment operations in a single, intuitive interface.

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

158 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements