Log In

Don't have an account? Sign up now

Lost Password?

Sign Up

Prev Next

Module 3: Collections & Generics

In enterprise programming, data is rarely a single value; itโ€™s usually a stream of thousands of records. Module 3 teaches you how to store, search, and manipulate groups of objects efficiently. Choosing the wrong collection can make your application slow (high latency), while the right one ensures high performance.

1. Arrays vs. Collections

The fundamental difference lies in memory flexibility.

  • Arrays (string[]): * Fixed Size: You must declare the size at the start (e.g., new string[5]). To add a 6th item, you have to create a brand new array and copy everything over.
    • Performance: Extremely fast for “indexing” (accessing item #3 directly).
  • Collections (e.g., List<T>): * Dynamic Size: They grow and shrink automatically as you add or remove items.
    • Rich Methods: They come with built-in functions like .Add(), .Remove(), .Sort(), and .Clear().

2. List, Dictionary, Stack, Queue

These are the “Big Four” data structures you will use 90% of the time in C#.

List<T>

The most commonly used collection. Itโ€™s an ordered list of items accessible by index.

  • Best for: When you need an ordered sequence and don’t mind a slight performance hit when inserting items in the middle.

Dictionary<TKey, TValue>

Stores data in Key-Value pairs. Think of a real dictionary: the “Word” is the Key, and the “Definition” is the Value.

  • Best for: High-speed lookups. If you have 1 million users and want to find one by their UserId, a Dictionary finds them almost instantly, whereas a List would have to check every single item.

Stack<T> (LIFO)

Last-In, First-Out. Imagine a stack of physical plates. The last plate you put on top is the first one you take off.

  • Use Case: “Undo” buttons in software or navigating back through browser history.

Queue<T> (FIFO)

First-In, First-Out. Imagine a line at a coffee shop. The first person in line is the first one served.

  • Use Case: Printing jobs or background task processing where orders must be handled in the order they arrived.

3. Generic Collections

In early versions of .NET, we used “ArrayLists” which could hold anything (integers, strings, and dogs all in one list). This was dangerous and slow.

Generics (represented by the <T> symbol) allow you to define a collection with a “Type Parameter.”

  • Type Safety: List<int> will refuse to accept a string. This catches errors at compile-time rather than crashing the app at runtime.
  • Performance: It avoids “Boxing and Unboxing” (converting value types to objects), which saves significant CPU cycles.

4. Custom Collections

Sometimes, a standard List isn’t enough. You might want a collection that automatically logs every time an item is added, or one that only allows unique values.

To create a custom collection, you usually:

  1. Inherit from Collection<T> or IEnumerable<T>.
  2. Override methods like InsertItem or RemoveItem to add your custom business logic.
  • Enterprise Example: A ReadOnlyEmployeeCollection that prevents any external code from modifying the company’s payroll list.

5. Performance Considerations (Big O Basics)

In professional software, we talk about Time Complexity.

  • List Lookup: If you search for a value in a List, itโ€™s O(n)โ€”meaning if there are 1,000 items, it might take 1,000 checks.
  • Dictionary Lookup: This is O(1)โ€”meaning it takes the same amount of time to find a key whether you have 10 items or 10 million.
  • Memory: Collections use more memory than Arrays because they maintain extra space to grow.

Outcome: Data Architecture Thinking

By the end of this module, you stop asking “How do I store this?” and start asking “How will I access this?”

  • Need to find items by ID? Use a Dictionary.
  • Need to process tasks in order? Use a Queue.
  • Just need a simple list to show on a screen? Use a List.

Leave a Comment

    ๐Ÿš€ Join Common Jobs Pro โ€” Referrals & Profile Visibility Join Now ร—
    ๐Ÿ”ฅ