Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Core Concepts: Classes and Objects

Introduction to Classes and Objects

In .NET, classes are the fundamental building blocks used to create objects. A class defines the data and behavior of its objects.

Key Concepts

  • Class: Blueprint for creating objects. It defines properties, methods, and events.
  • Object: Instance of a class that can be created and manipulated at runtime.
  • Encapsulation: Bundling data (fields) and methods (functions) that operate on the data into a single unit (class).

Example: Creating a Class and Object

Here's an example of a simple class and how to create objects from it in C#:


// Define a class
public class Car
{
    // Fields (data members)
    public string make;
    public string model;
    public int year;

    // Constructor
    public Car(string make, string model, int year)
    {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    // Method
    public void DisplayInfo()
    {
        Console.WriteLine($"Car: {year} {make} {model}");
    }
}

// Create objects
Car car1 = new Car("Toyota", "Camry", 2022);
Car car2 = new Car("Honda", "Accord", 2023);

// Use objects
car1.DisplayInfo(); // Output: Car: 2022 Toyota Camry
car2.DisplayInfo(); // Output: Car: 2023 Honda Accord
            

In this example, Car is a class that defines the blueprint for cars, and car1 and car2 are objects created from this class.

Constructors and Destructors

Constructors are special methods in a class used to initialize objects. Destructors are used to perform cleanup operations before an object is destroyed.

Example: Constructor and Destructor


public class Book
{
    public string title;
    public string author;

    // Constructor
    public Book(string title, string author)
    {
        this.title = title;
        this.author = author;
        Console.WriteLine($"Book '{title}' by {author} is created.");
    }

    // Destructor (finalizer)
    ~Book()
    {
        Console.WriteLine($"Book '{title}' by {author} is destroyed.");
    }
}

// Create and use an object
Book book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald");
// Output: Book 'The Great Gatsby' by F. Scott Fitzgerald is created.

// At the end of the program or when the object goes out of scope:
// Output: Book 'The Great Gatsby' by F. Scott Fitzgerald is destroyed.
            

In this example, the constructor initializes a Book object with a title and author, and the destructor cleans up resources when the object is no longer needed.

Access Modifiers

Access modifiers control the visibility and accessibility of class members (fields, methods, properties).

Example: Access Modifiers


public class Employee
{
    private string name; // Private field
    public int salary;   // Public field

    public Employee(string name, int salary)
    {
        this.name = name;
        this.salary = salary;
    }

    public void DisplayInfo()
    {
        Console.WriteLine($"Employee: {name}, Salary: {salary}");
    }
}

// Create and use an object
Employee emp1 = new Employee("John Doe", 50000);
emp1.DisplayInfo(); // Output: Employee: John Doe, Salary: 50000
            

In this example, name is a private field, accessible only within the Employee class, while salary is a public field accessible from outside the class.

Conclusion

Classes and objects are fundamental concepts in object-oriented programming (OOP) with .NET. They encapsulate data and behavior, provide a blueprint for creating objects, and enable code reuse and modularity.