C-SHARP - 3.2 Object and class

C# is an object-oriented language: almost everything we manipulate is an object. To create our own objects we first define what they look like with a class. A class is a blueprint declared once that can be instantiated as many times as we need with different values. Each instance shares the same structure but holds its own data, which is perfect for representing things like cars, users or products in our applications.

To make this concrete, imagine running a car garage. Every car has common characteristics with different values: a colour that varies from one car to another, a number of doors that can be 3, 4 or 5, a brand, a year. In the class we describe these properties and call them attributes. Starting from this single class, we can create several instances sharing the same attributes but with their own values: one car may be a 2019 blue Peugeot, another a 2021 grey Mercedes, and so on.

Classes also contain methods

  • Attributes describe the state of the object (brand, year, colour).
  • Methods describe what the object can do (drive, accelerate, brake).
  • An instance is a concrete object built from the class with its own data.

Methods are functions that belong to the class and let us act on the instance: for a car class we could write Drive, Accelerate or Reverse. Beyond modelling real-world entities, object-oriented programming helps us organise code, avoid repetition and build reusable building blocks. Instead of repeating fifty almost-identical blocks of code to handle many cars, we declare a single class and produce as many instances as we need with one line of code each. In the next video we open Visual Studio to see how to declare a class in C# and create our first instance.

Summary

This lesson introduces the fundamental concepts of Object-Oriented Programming (OOP) in C#, explaining how classes serve as blueprints for creating objects with shared attributes and methods. Using a car garage scenario, the lesson demonstrates how to define class attributes (brand, year, color) and methods (accelerate, reverse), illustrating how a single class template can efficiently generate multiple object instances with different property values. OOP enables developers to write reusable, organized code by avoiding repetition and following the principle of defining logic once and reusing it throughout the program.

Key points

  • C# is an object-oriented language where everything is an object; classes are used to define what objects look like
  • Classes act as templates (or molds) that define attributes (properties) and methods (functions) shared by all instances
  • Instances are individual objects created from a class; each instance has the same attributes but with potentially different values
  • Methods are functions that belong to a class and allow you to perform actions on object instances (e.g., accelerate, reverse)
  • OOP eliminates code repetition by defining shared logic once in a class, then creating multiple instances without rewriting the same code
  • Classes improve code organization and reusability, enabling developers to build scalable, maintainable programs

FAQ

What is the difference between a class and an object in C#?

A class is a template or blueprint that defines what an object looks like; an object (or instance) is a concrete implementation of that class created from the blueprint with specific values for its attributes.

What are attributes and methods in the context of classes?

Attributes are properties that describe an object's characteristics (e.g., brand, year, color for a car), while methods are functions defined within a class that allow you to perform actions on the object (e.g., accelerate, reverse).

Why is Object-Oriented Programming important in C#?

OOP helps developers write reusable, organized code by defining logic once in a class and creating multiple instances without repeating code, making programs more maintainable and scalable.