What are key-based I/O collections in C#?

Key-based I/O collections in C# are collections that store data as key-value pairs, where you can access values using their associated keys. The primary example of this is the SortedList class, which maintains elements in sorted order based on the keys.

Syntax

Following is the syntax for declaring a generic SortedList −

SortedList<TKey, TValue> listName = new SortedList<TKey, TValue>();

Following is the syntax for adding key-value pairs −

listName.Add(key, value);
listName[key] = value;  // alternative syntax

Key Features of SortedList

  • Stores key-value pairs sorted by keys in ascending order

  • Allows access by both key and index

  • Keys must be unique within the collection

  • Provides fast lookups using binary search

SortedList Structure Key-Value Pairs (Sorted by Key) Sub1 Economics Sub2 Chemistry Keys (sorted) Values Access: sortedList["Sub1"] returns "Economics"

Using Generic SortedList

Example

using System;
using System.Collections.Generic;

class Program {
    static void Main(string[] args) {
        SortedList<string, string> subjects = new SortedList<string, string>();

        subjects.Add("Sub4", "English");
        subjects.Add("Sub1", "Economics");
        subjects.Add("Sub3", "Business Studies");
        subjects.Add("Sub2", "Accountancy");

        Console.WriteLine("Count: " + subjects.Count);
        Console.WriteLine("Capacity: " + subjects.Capacity);
        Console.WriteLine("\nSorted Key-Value Pairs:");

        foreach (KeyValuePair<string, string> pair in subjects) {
            Console.WriteLine(pair.Key + ": " + pair.Value);
        }

        Console.WriteLine("\nAccessing by key:");
        Console.WriteLine("Sub2 = " + subjects["Sub2"]);
    }
}

The output of the above code is −

Count: 4
Capacity: 4
 
Sorted Key-Value Pairs:
Sub1: Economics
Sub2: Accountancy
Sub3: Business Studies
Sub4: English
 
Accessing by key:
Sub2 = Accountancy

Using Non-Generic SortedList

Example

using System;
using System.Collections;

class Program {
    static void Main(string[] args) {
        SortedList s = new SortedList();

        s.Add("Sub1", "Economics");
        s.Add("Sub2", "Accountancy");
        s.Add("Sub3", "Business Studies");
        s.Add("Sub4", "English");

        Console.WriteLine("Capacity = " + s.Capacity);

        // 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 −

Capacity = 16
Sub1: Economics
Sub2: Accountancy
Sub3: Business Studies
Sub4: English

Common Operations

Example

using System;
using System.Collections.Generic;

class Program {
    static void Main(string[] args) {
        SortedList<int, string> numbers = new SortedList<int, string>();

        numbers.Add(3, "Three");
        numbers.Add(1, "One");
        numbers.Add(2, "Two");

        Console.WriteLine("Original SortedList:");
        foreach (var item in numbers) {
            Console.WriteLine(item.Key + " = " + item.Value);
        }

        Console.WriteLine("\nContains key 2: " + numbers.ContainsKey(2));
        Console.WriteLine("Contains value 'Two': " + numbers.ContainsValue("Two"));

        numbers.Remove(2);
        Console.WriteLine("\nAfter removing key 2:");
        foreach (var item in numbers) {
            Console.WriteLine(item.Key + " = " + item.Value);
        }
    }
}

The output of the above code is −

Original SortedList:
1 = One
2 = Two
3 = Three

Contains key 2: True
Contains value 'Two': True

After removing key 2:
1 = One
3 = Three

Conclusion

Key-based I/O collections like SortedList in C# provide efficient storage and retrieval of key-value pairs in sorted order. They are ideal when you need both fast key-based lookups and maintaining elements in a sorted sequence based on keys.

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

259 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements