Module 2: Advanced C# & OOP Concepts

Master advanced C# concepts and object-oriented programming principles.

Back to Course|6 hours|Intermediate

Advanced C# & OOP Concepts

Master advanced C# concepts and object-oriented programming principles.

Progress: 0/6 topics completed0%

Select Topics Overview

Classes, Objects & Inheritance

Master object-oriented programming concepts including classes, objects, and inheritance in C#

Content by: Vaibhav Nakrani

.Net Developer

Connect

Understanding Classes and Objects

Classes are blueprints for creating objects. They define the structure and behavior that objects of that class will have. Objects are instances of classes that contain actual data and can perform actions.

Class Definition and Object Creation

Code Example
// Class definition
public class Car
{
    // Fields (attributes)
    public string Brand { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
    public double Speed { get; set; }

    // Constructor
    public Car(string brand, string model, int year)
    {
        Brand = brand;
        Model = model;
        Year = year;
        Speed = 0;
    }

    // Methods (behavior)
    public void Accelerate(double increment)
    {
        Speed += increment;
        Console.WriteLine($"{Brand} {Model} is now going {Speed} km/h");
    }

    public void Brake(double decrement)
    {
        Speed = Math.Max(0, Speed - decrement);
        Console.WriteLine($"{Brand} {Model} slowed down to {Speed} km/h");
    }

    public void DisplayInfo()
    {
        Console.WriteLine($"Car: {Year} {Brand} {Model}");
    }
}

// Creating objects
Car myCar = new Car("Toyota", "Camry", 2023);
Car yourCar = new Car("Honda", "Civic", 2022);

// Using objects
myCar.DisplayInfo();
myCar.Accelerate(50);
yourCar.Accelerate(30);
Swipe to see more code

🎯 Practice Exercise

Test your understanding of this topic:

Ready for the Next Module?

Continue your learning journey and master the next set of concepts.

Continue to Module 3