C# Multiple Classes and Objects

Welcome to The Coding College! In this tutorial, we will explore how to use multiple classes and objects in C#. By the end of this post, you’ll understand how to create and use multiple classes in a single program and make them interact efficiently.

Understanding Multiple Classes

In C#, you can structure your code by creating multiple classes. This helps to separate concerns and organize your program better. Each class can have its fields, properties, and methods, and you can create objects of those classes to use them together.

Why Use Multiple Classes?

  1. Modularity: Makes your code more manageable and easier to debug.
  2. Reusability: Classes can be reused across different parts of your program.
  3. Encapsulation: Keeps data and methods in separate, self-contained units.

Creating Multiple Classes

You can define as many classes as you need in a single program or across multiple files.

Example:

Let’s create a program with two classes, Car and Driver.

using System;

public class Car
{
    public string Brand;
    public string Model;

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

public class Driver
{
    public string Name;
    public int Age;

    public void DisplayDriverInfo()
    {
        Console.WriteLine($"Driver: {Name}, Age: {Age}");
    }
}

public class Program
{
    public static void Main()
    {
        // Create an object of Car
        Car car = new Car();
        car.Brand = "Tesla";
        car.Model = "Model X";

        // Create an object of Driver
        Driver driver = new Driver();
        driver.Name = "John Doe";
        driver.Age = 30;

        // Display information
        car.DisplayCarInfo();    // Output: Car: Tesla Model X
        driver.DisplayDriverInfo(); // Output: Driver: John Doe, Age: 30
    }
}

Interaction Between Multiple Classes

Classes can interact with each other, and one class can use objects of another class.

Example:

Here’s how a Driver class can access and use a Car object.

using System;

public class Car
{
    public string Brand;
    public string Model;

    public Car(string brand, string model)
    {
        Brand = brand;
        Model = model;
    }

    public void Drive()
    {
        Console.WriteLine($"The car {Brand} {Model} is being driven.");
    }
}

public class Driver
{
    public string Name;

    public void DriveCar(Car car)
    {
        Console.WriteLine($"{Name} is driving the {car.Brand} {car.Model}.");
        car.Drive();
    }
}

public class Program
{
    public static void Main()
    {
        // Create a Car object
        Car car = new Car("BMW", "X5");

        // Create a Driver object
        Driver driver = new Driver();
        driver.Name = "Alice";

        // Driver interacts with Car
        driver.DriveCar(car);
        // Output:
        // Alice is driving the BMW X5.
        // The car BMW X5 is being driven.
    }
}

Organizing Classes in Separate Files

In larger programs, it’s a good practice to place each class in its own file for better organization. For example:

  • Car.cs
public class Car
{
    public string Brand;
    public string Model;

    public void DisplayCarInfo()
    {
        Console.WriteLine($"Car: {Brand} {Model}");
    }
}
  • Driver.cs
public class Driver
{
    public string Name;
    public int Age;

    public void DisplayDriverInfo()
    {
        Console.WriteLine($"Driver: {Name}, Age: {Age}");
    }
}
  • Program.cs
public class Program
{
    public static void Main()
    {
        Car car = new Car { Brand = "Audi", Model = "Q7" };
        Driver driver = new Driver { Name = "Michael", Age = 35 };

        car.DisplayCarInfo();
        driver.DisplayDriverInfo();
    }
}

Benefits of Using Multiple Classes

  1. Readability: Code is easier to read and understand.
  2. Team Collaboration: Multiple classes allow teams to work on different parts of the application independently.
  3. Scalability: Easy to add new functionality by adding new classes.

Real-World Use Case

Imagine you’re building a Library Management System. You might have multiple classes like:

  • Book: Represents a book with properties like Title, Author, and ISBN.
  • Member: Represents a library member with properties like Name, MembershipID.
  • Library: Manages the collection of books and members.

Here’s how you might structure it:

using System;

public class Book
{
    public string Title;
    public string Author;

    public void DisplayBookInfo()
    {
        Console.WriteLine($"Book: {Title} by {Author}");
    }
}

public class Member
{
    public string Name;

    public void BorrowBook(Book book)
    {
        Console.WriteLine($"{Name} borrowed the book: {book.Title}");
    }
}

public class Program
{
    public static void Main()
    {
        // Create a Book object
        Book book = new Book { Title = "C# Programming", Author = "John Doe" };

        // Create a Member object
        Member member = new Member { Name = "Jane Smith" };

        // Member borrows a book
        book.DisplayBookInfo();
        member.BorrowBook(book);
    }
}

Conclusion

Using multiple classes and objects in C# allows for clean, modular, and maintainable code. It makes your application more scalable and adheres to good coding practices like OOP principles.

For more tutorials on coding and programming, visit The Coding College, your go-to platform for learning.

Leave a Comment