C# Collections

Overview of C# Collections (List, Set, Dictionary)

C# Collections form the backbone of efficient data management, allowing developers to store, access, and manipulate data seamlessly. In C#, three primary collection types are widely used: List, Set, and Dictionary.

  • Lists: Lists provide a dynamic way to organize elements in a specific order, offering operations like adding, removing, and searching elements effortlessly.
  • Sets: Sets ensure that each element in the collection is unique, making them perfect for maintaining a distinct collection of items.
  • Dictionaries: Dictionaries facilitate a powerful key-value pair mechanism, enabling rapid lookup and retrieval of values based on their corresponding keys.

Working with List<T>, HashSet<T>, Dictionary<TKey, TValue>, etc

Let’s dive into practical examples of each collection type to understand their utility better:

Example: List<T>

List<string> names = new List<string>();
names.Add(“Alice”);
names.Add(“Bob”);
names.Add(“Charlie”);

foreach (string name in names)
{
Console.WriteLine(“Hello, ” + name + “!”);
}
Example: HashSet<T>

HashSet<int> numbers = new HashSet<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
numbers.Add(2); // Ignored due to the unique nature of sets

foreach (int number in numbers)
{
Console.WriteLine(“Number: ” + number);
}
Example: Dictionary<TKey, TValue>

Dictionary<string, int> ageMap = new Dictionary<string, int>();
ageMap.Add(“Alice”, 30);
ageMap.Add(“Bob”, 25);
ageMap.Add(“Charlie”, 28);

Console.WriteLine(“Bob’s age: ” + ageMap[“Bob”]);

Iterators and LINQ (Language-Integrated Query)

Enhancing the capabilities of C# collections, iterators and LINQ (Language-Integrated Query) add a whole new dimension to data manipulation.

  • Iterators: Leveraging the yield return statement, iterators enable the lazy evaluation of collection elements, optimizing memory usage.

Example: Iterator

IEnumerable<int> GenerateNumbers()
{
for (int i = 1; i <= 10; i++)
{
yield return i;
}
}

foreach (int number in GenerateNumbers())
{
Console.WriteLine(“Number: ” + number);
}
LINQ: Language-Integrated Query simplifies querying operations on collections, making data retrieval, filtering, sorting, and aggregation tasks a breeze.
Example: LINQ

List<int> numbers = new List<int> { 2, 5, 8, 11, 15 };
IEnumerable<int> evenNumbers = numbers.Where(n => n % 2 == 0);

foreach (int number in evenNumbers)
{
Console.WriteLine(“Even Number: ” + number);
}

Conclusion:

C# Collections provide a robust toolkit for developers to efficiently manage data. From ordered Lists, unique Sets, to key-value based Dictionaries, each collection type serves a unique purpose. Leveraging iterators and LINQ further enriches the data manipulation experience, making C# Collections a powerful asset in every programmer’s toolkit. By mastering C# Collections, you can unlock the true potential of data management, elevate your coding prowess, and build more efficient and scalable applications. So, embrace the power of C# Collections and let your code soar to new heights!

Leave a Comment