Tuesday, June 30, 2026

C# Dictionary Initialization approaches

1. The most common approach is collection initializer.
// collection initializer syntax
// each key-value appear to be elements of set
Dictionary<int, string> sets = new Dictionary<int, string>
{
{101,"Red" },
{102,"Green" },
{103,"Blue" },
{104,"Cyan" },
{105,"Orange" },
};
2. The second approach is to use target type with new().
// Target type syntax
Dictionary<int, string> sets2 = new()
{
{101,"Red" },
{102,"Green" },
{103,"Blue" },
{104,"Cyan" },
{105,"Orange" },
};
3. The third approach is to use var with new() constructor
// var syntax
var sets3 = new Dictionary<int, string>()
{
{101,"Red" },
{102,"Green" },
{103,"Blue" },
{104,"Cyan" },
{105,"Orange" },
};
4. The fourth approach is to use index style where index is key
// index style syntax
Dictionary<int, string> indices1 = new Dictionary<int, string>
{
[101] = "Red",
[102] = "Green",
[103] = "Blue",
[104] = "Cyan",
[105] = "Orang",
};
5. The fifth approach is to use index style with collection expression
// index style syntax, collection expression
Dictionary<int, string> indices2 = new()
{
[101] = "Red",
[102] = "Green",
[103] = "Blue",
[104] = "Cyan",
[105] = "Orang",
};
6. The sixth approach is to use index style with var keyword
// index style syntax, collection expression
var indices3 = new Dictionary<int, string>()
{
[101] = "Red",
[102] = "Green",
[103] = "Blue",
[104] = "Cyan",
[105] = "Orang",
};
}
}

C# List is generally preferred over StringCollection. Why

In modern C# development, List<string> is almost universally preferred over StringCollection. The short answer is that StringCollection is a relic of the past, while List<T> represents the modern, type-safe standard.

Here is a breakdown of exactly why List<string> wins across the board:
1. Generics vs. Non-Generics (The History Lesson)
  • StringCollection belongs to the System.Collections.Specialized namespace and was introduced in .NET 1.1—before Generics existed. It internally stores data as an array of object types.
  • List<T> was introduced in .NET 2.0 with the System.Collections.Generic namespace.
  • Because List<string> uses generics, the compiler knows exactly what type of data is inside the list at compile time.
2. Performance and Memory Efficiency
  • Because StringCollection handles data as object types behind the scenes, it can incur performance penalties. While strings are reference types (meaning they don't suffer from traditional "boxing" like integers do), StringCollection still requires frequent casting from object back to string when you retrieve items.
  • List<string> avoids all casting overhead, operating directly on the string type, making it faster and lighter on memory.
3. LINQ Support
  • List<string> implements IEnumerable<string>, which means it works seamlessly out-of-the-box with all LINQ methods (.Where(), .Select(), .OrderBy(), etc.).
  • StringCollection only implements the non-generic IEnumerable. If you want to use LINQ with it, you are forced to call .Cast<string>() first, adding unnecessary boilerplate code:
// Modern and clean with List<string>
var result = myList.Where(s => s.StartsWith("A")).ToList();

// Clunky with StringCollection
var result = myStringCollection.Cast<string>().Where(s => s.StartsWith("A")).ToList();

4. Richer API and Ecosystem Fit
  • List<T> comes with a massive array of built-in utility methods that StringCollection lacks, such as .AddRange(), .RemoveAll(), .Find(), .TrueForAll(), and .Sort().
  • Furthermore, almost every modern third-party library, NuGet package, and internal .NET API expects generic collections (List<T>, IEnumerable<T>, IList<T>). Using StringCollection forces you to constantly convert your data back and forth just to interact with modern APIs.
Summary Comparison
Feature List<string> StringCollection
Introduced .NET 2.0 (Modern standard) .NET 1.1 (Legacy/Obsolete)
Type Safety Compile-time guaranteed Run-time checked
LINQ Support Native Requires .Cast<string>()
Performance Optimized Slower due to casting
Example of StringCollection
using System.Collections.Specialized;
class Program
{
    static void Main(string[] args)
    {
        StringCollection words = new StringCollection();
        words.Add("Hello");
        words.Add("World");
        words.AddRange(new string[] { "Ajeet", "Kumar", "Gupta" });
        if (words.Contains("Kumar"))
        {
            Console.WriteLine("Total words:" + words.Count);//5
        }
        words.Clear();
        Console.WriteLine("Total words:" + words.Count);//0
        words.Insert(0, "Hello");
        words.Insert(1, "Kumar");
        words.Insert(2, "Ajeet");
        words.Insert(3, "Gupta");
        words.Insert(3, "Gaytri");
        Console.WriteLine("Total words:" + words.Count);//5
        words.Remove(words[1]);
        Console.WriteLine("Total words:" + words.Count);//4
        words.RemoveAt(3);
        Console.WriteLine("Total words:" + words.Count);//3

        // iteration using indexer
        for (int i = 0; i < words.Count; i++)
        {
            Console.Write(words[i] + " "); // Hello Ajeet Gaytri
        }
        Console.WriteLine();
        // iteration using StringEnumerator
        StringEnumerator enumerator = words.GetEnumerator();
        while (enumerator.MoveNext())
        {
            Console.Write(enumerator.Current + " ");
        }
        Console.WriteLine();
        // StringCollection implements IEnumerable
        foreach (var word in words)
        {
            Console.Write(word + " ");
        }
    }
}

Hot Topics