Collections

9. Collections#

Arrays have fixed sizes. When the amount of data is unknown or changes while the program runs, C# provides dynamic collection types. This chapter introduces List<T> and Dictionary<TKey, TValue>, two of the most widely used collections.

  • List: A List<T> stores an ordered sequence that can grow or shrink.

  • Dictionary: A Dictionary<TKey, TValue> maps keys to values for fast lookup.

  • Generics: Generic type parameters keep collections type-safe while making them reusable.

  • Access pattern: Choosing a collection depends on whether you need order, lookup by key, insertion, removal, or traversal.

Collections let programs handle real data sizes and changing workloads without manually managing every storage detail.

Video

C# Lists 📃 by Bro Code.

This video remains hosted on YouTube and is the property of its original creator or rightsholder; it is embedded here via YouTube’s official player for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Create and populate List<T> values using common list operations

  2. Use Dictionary<TKey, TValue> to store and retrieve key-value pairs

  3. Iterate over collections with foreach loops

  4. Explain how generics make collections type-safe

  5. Choose between arrays, lists, and dictionaries for common data tasks

Chapter Flow