Object-Oriented Programming: Objects and Classes

Object-Oriented Programming is a programming paradigm used in Computer Science and the world of Software Engineering. It is indeed something important to learn, and understand. In order to put things into perspective, an Object is something that derives from a class. The class is a blueprint for Objects.

An example of this would be to create a class for cars. What does a car have? we could add in the potential data types such as:

  • Year (Year it was released)
  • Make (The company of the car)
  • Model (The brand of the company)
  • Type (The type of car: SUV, Sedan, Coupe)

Listed below is how you could implement a class in C#:

public class Car {
  public int CarId {get; set;}
  public int Year {get; set;}
  public string Make {get; set;}
  public string Model {get; set;}
  public string Type {get; set;}

The Object is an instance of a Class, and has it’s own values along with properties. We can create an object using the Car Class by the following below:

var car = new Car();

this creates a car variable based on the Car type and allows you to modify and access data within the car object. However, once the application is terminated, all data for this specific car object would be lost unless the data was stored in a specific area such as a database. the class however is hardcoded within the application, and therefore it remains.

From here, we can use the object created to add the actual values to the object:

var car = new Car();

car.Year = 2013;
car.Make = "Honda";
car.Model = "Civic"; 
car.Type = "Coupe";

Similarly, we can clean up the syntax, as long as we ensure that the Car class that we’ve constructed is imported within our program:

Car car = new Car
   Year = 2013, 
   Make = "Honda",
   Model = "Civic",
   Type = "Coupe"


To conclude, a class is defined as a blueprint, and an object derives from the class. In this example, we defined a class named Car, and from that Car class, we derived a car object and gave the attributes of “Honda”, “Civic”, “2013”, and “Coupe” which were values that populated the car object.

It’s important to keep in mind that Objects are “reference types”, they hold the reference to the data instead of the data itself, which are different from “value types” which directly hold their data. for example, consider the following below:

int year = 2003;

int yearTwo = year; 

yearTwo = 2020;

If we compare theses results the following occurs:

  • declare an integer named year and set it to 2003
  • declare another integer named yearTwo and set it equal to the value held in the year variable
  • assign the value 2020 to the yearTwo variable
  • End Result: year = 2003 | yearTwo = 2020

Now let’s take a look at an object scenario:

var car = new Car(); 
car.Make = "Honda"; 

var carTwo = new Car(); 
carTwo.Make = car;

carTwo.Make = "Toyota"; 

In this scenario, the reference of car and carTwo would be Toyota, due to the fact that they both reference the same value.