Creating a synchronized wrapper for a SortedList object in C#

A synchronized wrapper for a SortedList object provides thread-safe access to the collection in multi-threaded environments. The SortedList.Synchronized() method creates a wrapper that ensures only one thread can access the collection at a time.

Syntax

Following is the syntax for creating a synchronized wrapper −

SortedList synchronizedList = SortedList.Synchronized(originalList);

To check if a SortedList is synchronized, use the IsSynchronized property −

bool isSync = sortedList.IsSynchronized;

Creating a Synchronized SortedList

The following example demonstrates how to create a synchronized wrapper and verify its synchronization status −

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        SortedList sortedList = new SortedList();
        sortedList.Add("1", "Tom");
        sortedList.Add("2", "Ryan");
        sortedList.Add("3", "Nathan");
        
        Console.WriteLine("Original SortedList elements...");
        foreach(DictionaryEntry d in sortedList) {
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }
        
        Console.WriteLine("Original SortedList is synchronized? = " + sortedList.IsSynchronized);
        
        SortedList sortedList2 = SortedList.Synchronized(sortedList);
        Console.WriteLine("Synchronized wrapper is synchronized? = " + sortedList2.IsSynchronized);
    }
}

The output of the above code is −

Original SortedList elements...
Key = 1, Value = Tom
Key = 2, Value = Ryan
Key = 3, Value = Nathan
Original SortedList is synchronized? = False
Synchronized wrapper is synchronized? = True

Thread-Safe Operations with Synchronized Wrapper

The synchronized wrapper ensures thread-safe access to all operations including add, remove, and enumeration −

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        SortedList originalList = new SortedList();
        originalList.Add("A", "Apple");
        originalList.Add("B", "Banana");
        originalList.Add("C", "Cherry");
        
        SortedList syncList = SortedList.Synchronized(originalList);
        
        Console.WriteLine("Synchronized SortedList operations:");
        syncList.Add("D", "Date");
        syncList["E"] = "Elderberry";
        
        foreach(DictionaryEntry entry in syncList) {
            Console.WriteLine($"Key = {entry.Key}, Value = {entry.Value}");
        }
        
        Console.WriteLine($"Count: {syncList.Count}");
        Console.WriteLine($"Contains key 'C': {syncList.ContainsKey("C")}");
        Console.WriteLine($"Is synchronized: {syncList.IsSynchronized}");
    }
}

The output of the above code is −

Synchronized SortedList operations:
Key = A, Value = Apple
Key = B, Value = Banana
Key = C, Value = Cherry
Key = D, Value = Date
Key = E, Value = Elderberry
Count: 5
Contains key 'C': True
Is synchronized: True

When to Use Synchronized Wrappers

Scenario Recommendation
Single-threaded applications Use regular SortedList for better performance
Multi-threaded read/write operations Use SortedList.Synchronized() wrapper
Modern .NET applications Consider ConcurrentDictionary or manual locking

Key Points

  • The SortedList.Synchronized() method returns a thread-safe wrapper, not a new collection.

  • Both the original and synchronized wrapper reference the same underlying data.

  • Changes made through either reference are visible in both.

  • Only operations through the synchronized wrapper are thread-safe.

Conclusion

The SortedList.Synchronized() method creates a thread-safe wrapper around an existing SortedList, enabling safe concurrent access in multi-threaded applications. While useful for legacy code, modern applications should consider using concurrent collections from System.Collections.Concurrent namespace for better performance.

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

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements