Module 4 - Chapter 3 : Collections in C#

Collections are data structures that hold objects in memory. In C#, collections are incredibly versatile and can be used to store, manipulate, and manage data efficiently. Unlike arrays, which have a fixed size, collections can dynamically resize, offering more flexibility. This chapter introduces some of the most commonly used collection types in the .NET Framework, including their use cases, benefits, and key operations.

Mar 29, 2024

Collections in C#

Collections are data structures that hold objects in memory. In C#, collections are incredibly versatile and can be used to store, manipulate, and manage data efficiently. Unlike arrays, which have a fixed size, collections can dynamically resize, offering more flexibility. This chapter introduces some of the most commonly used collection types in the .NET Framework, including their use cases, benefits, and key operations.

Arrays vs. Collections

While arrays are useful for storing fixed-size, homogeneous data sets, collections offer more functionality, such as dynamic resizing and the ability to store heterogeneous objects. Collections also provide various methods for item manipulation, making them more flexible and powerful for data management.

System.Collections.Generic

The System.Collections.Generic namespace includes several collection classes that are type-safe and can store any data types. Using generic collections helps to avoid boxing and unboxing and improves performance.

List<T>

The List<T> class represents a strongly typed list of objects that can be accessed by index. It provides methods to search, sort, and manipulate lists.
List<string> names = new List<string>(); names.Add("Alice"); names.Add("Bob"); names.Add("Charlie");

Dictionary<TKey, TValue>

A Dictionary<TKey, TValue> is a collection of key/value pairs that are organized based on the key. It is useful for fast lookups by key.
Dictionary<int, string> users = new Dictionary<int, string>(); users.Add(1, "Alice"); users.Add(2, "Bob");

Queue<T>

A Queue<T> represents a first-in, first-out (FIFO) collection of objects. It is ideal for storing objects in the order they were added, simulating a queue.
Queue<string> tasks = new Queue<string>(); tasks.Enqueue("Task1"); tasks.Enqueue("Task2");

Stack<T>

A Stack<T> represents a last-in, first-out (LIFO) collection of objects. It is used to store objects in reverse order from which they were added, simulating a stack.
Stack<string> books = new Stack<string>(); books.Push("Book1"); books.Push("Book2");

System.Collections Namespace

This namespace contains non-generic collection types. However, it's generally recommended to use the generic collections for type safety and better performance.

ArrayList

An ArrayList is similar to a List<T>, but it can hold items of any data type. Due to its non-generic nature, it's less efficient than List<T>.
ArrayList myArrayList = new ArrayList(); myArrayList.Add(1); // Integer myArrayList.Add("Hello"); // String

Hashtable

A Hashtable stores key/value pairs, similar to Dictionary<TKey, TValue>, but without type safety.
Hashtable myHashtable = new Hashtable(); myHashtable.Add("ID1", "Alice"); myHashtable.Add("ID2", "Bob");

LINQ and Collections

Language Integrated Query (LINQ) can be used with collections to perform powerful data queries and manipulations more succinctly and readably.
var filteredNames = names.Where(name => name.StartsWith("A"));

Summary

Collections in C# provide a flexible way to work with groups of objects. The .NET Framework offers a variety of collection classes for different purposes, including generic collections for type safety and performance. Understanding how to use these collections effectively is essential for managing data in your C# applications, enabling efficient storage, retrieval, and manipulation of objects.