1 2_introducing_collections_and_arrays
jason.zhu edited this page 2021-05-11 09:53:28 +10:00

Introducting Collections and Arrays

What is a Collection?

Def: Collection is a data type whose purpose is to group same type of data together. So you can deal with a lot obj at same time.

Why collection is crucial for real data:

  • Real data normally comprises lots of objects (e.g. sales, employees)
  • Collection let you treat lots of objects as one single object

Many collections are available in .NET, 3 types are introduced here:

  • Array
  • List
  • Dictionary

The Array: a Fixed Size Ordered Collection

Reference:

Array Characteristics:

  • Fixed size: size of array is fixed once de
  • Ordered

Syntax:

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

Enumerating an Array

Use case: iterate through every element within array. Note array stores element in order

Syntax:

foreach (var car in cars) 
{
    Console.WriteLine(car);
}

Some Collection Terminology

  • Element (Item): an object (or struct) in a collection
  • Enumerate: Go through each item in turn