Chapter 7: Arrays and Collections (Basics)

1. What is an Array? (Super Simple Analogy)

Think of an array like a row of numbered boxes in a shelf:

  • All boxes are the same type (all hold integers, or all hold strings, etc.)
  • Each box has a number (called index) starting from 0
  • You can put something in each box and get it back later by its number

2. Single-Dimensional Arrays (The Most Common)

Syntax to declare and initialize:

C#

Real example – Store and print student scores:

C#

3. Multi-Dimensional Arrays (Like a Table / Grid)

Rectangular 2D array – all rows have the same length

C#

4. Jagged Arrays (Array of Arrays – Rows Can Have Different Lengths)

Very useful when rows have different sizes.

C#

5. Introduction to Generic Collections (Much Better Than Arrays!)

Arrays are fixed size and not very flexible. Modern C# uses generic collections from System.Collections.Generic – they can grow, shrink, and are much easier to use.

You must add this line at the top of your file:

C#

A. List<T> – The Most Common Collection (Like a Dynamic Array)

C#

B. Dictionary<TKey, TValue> – Key-Value Pairs (Like a Phone Book)

C#

Mini-Project: Student Grade Manager

C#

Quick Comparison Table

Feature Array List<T> Dictionary<TKey,TValue>
Size Fixed Dynamic (grow/shrink) Dynamic
Access by Index (0,1,2…) Index Key
Ordered? Yes Yes No (but can use OrderedDict)
Duplicate keys/values? Allowed Allowed Keys must be unique
Most common use Fixed data, performance Most lists of items Lookup by name/ID

Your Homework (Super Fun & Practical!)

  1. Create a new console project called CollectionPlayground
  2. Make a Shopping List Manager:
    • Use a List<string> for items
    • Let user add 5 items (loop + Add)
    • Let user remove one item by name
    • Print the final list with numbers (1. Milk, 2. Bread…)
  3. Bonus: Create a Dictionary<string, decimal> for item → price
    • Calculate total cost of shopping

Next lesson: User Input and Output in Depth + Exception Handling Basics – we’re going to make programs that are safe and user-friendly!

You’re doing absolutely amazing! 🎉 Any part confusing? Want more examples with arrays or collections? Just tell me — I’m right here for you! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *