Practical 3: Objects

CAB201 - Programming Principles

School of Computer Science, Faculty of Science

Arrays - Create and Access

An array is a collection of variables of the same type.

int[] numbers = new int[5];
string[] shoppingList = new string[10];

Access (read / write) elements using an index, starting at 0.

Console.WriteLine(numbers[0]); // 0
shoppingList[0] = "Milk";

Find array length using Length property.

Console.WriteLine(numbers.Length); // 5
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Arrays - Iteration

Use a for loop to iterate through an array.

int[] numbers = new int[5] { 1, 2, 3, 4, 5 };
for (int i = 0; i < numbers.Length; i++) {
    Console.WriteLine(numbers[i]);
}

Or a foreach loop.

string[] shoppingList = new string[3] { "Milk", "Bread", "Eggs" };
foreach (string item in shoppingList) {
    Console.WriteLine(item);
}
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Classes and Objects - Creating a Class

A class is a blueprint for creating objects (e.g., there are different Books, but they all have some Title, Author, PublishedYear, etc. just different values).

class Book {
    // This is bad practice by the way - don't make fields public.
    public string title;
    public int publishedYear;
}

An object is a specific instance of a class (like my specific Book).

Book myBook = new Book();
myBook.title = "Another C# Book";
myBook.publishedYear = 2024;
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Classes and Objects - Members

A class can have fields (hold data) and methods (perform actions). There's also properties, but we'll get to that later.

class Book {
    // Fields
    public string title;
    public int publishedYear;
    // A method
    public void Read(int page) {
        Console.WriteLine($"Reading page {page} of {title}");
    }
}
Book myBook = new Book();
myBook.Read(10);  // "Reading page 10 of Another C# Book"
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Classes and Objects - Visibility

The public keyword from the previous slide is an example of visibility. It determines where a field or method can be accessed from.

class Book {
  public string title;            // Bad practice
  private int publishedYear;      // Good practice - fields should be private
  public void Read(int page) {
    Console.WriteLine($"Reading page {page} of {title}");
  }
}
Book myBook = new Book();
myBook.title = "Another C# Book";                 // This is fine
myBook.Read(10);                                  // This is fine
myBook.publishedYear = 2024;                      // Compile error
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Classes and Objects - Constructors

A constructor is a special method that is called when an object is created (with the new keyword). It's used to initialise the object with necessary values.

class Book {
    private string title;
    private int publishedYear;
    public Book(string title, int publishedYear) {
        this.title = title; // this.title refers to the field, instead of the parameter
        this.publishedYear = publishedYear;
    }
}
Book myBook = new Book("Another C# Book", 2024);

Using a constructor, it is possible to assign values to private fields.

CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Classes and Objects - Overloading Constructors

A class can have multiple constructors, which is called overloading.

class Book {
    private string title;
    private int publishedYear;
    public Book(string title, int publishedYear) {
        this.title = title;
        this.publishedYear = publishedYear;
    }
    public Book() {
        this.title = "Unknown";
        this.publishedYear = 0;
    }
}
Book myBook1 = new Book("Another C# Book", 2024);
Book myBook2 = new Book(); // "Unknown", 0
CAB201 - Programming Principles
School of Computer Science, Faculty of Science