.NET Documentation

Classes and Objects

Classes are the fundamental building blocks of object-oriented programming (OOP) in .NET. They serve as blueprints for creating objects, which encapsulate data (fields) and behavior (methods).

Defining a Class

A class is defined using the class keyword, followed by the class name. Members of the class (fields and methods) are declared within curly braces. Access modifiers like public, private, and protected control the visibility of class members.

Example: A Simple `Car` Class


public class Car
{
    // Fields (data members)
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
    private int speed; // Private field to encapsulate speed

    // Constructor
    public Car(string make, string model, int year)
    {
        Make = make;
        Model = model;
        Year = year;
        speed = 0; // Initialize speed to 0
    }

    // Methods (behavior)
    public void Accelerate(int increment)
    {
        speed += increment;
        Console.WriteLine($"Accelerating. Current speed: {speed} mph");
    }

    public void Brake(int decrement)
    {
        if (speed - decrement >= 0)
        {
            speed -= decrement;
        }
        else
        {
            speed = 0;
        }
        Console.WriteLine($"Braking. Current speed: {speed} mph");
    }

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

    public int GetCurrentSpeed()
    {
        return speed;
    }
}
            

Creating Objects (Instances)

Objects are created from classes using the new keyword. This process is called instantiation. Each object is an independent instance of the class, with its own set of data.

Example: Instantiating and Using the `Car` Class


// In another part of your code, e.g., a Main method:
Car myCar = new Car("Toyota", "Camry", 2023);

myCar.DisplayInfo(); // Output: Car: 2023 Toyota Camry

myCar.Accelerate(50); // Output: Accelerating. Current speed: 50 mph
myCar.Brake(20);    // Output: Braking. Current speed: 30 mph

Console.WriteLine($"My car's current speed is: {myCar.GetCurrentSpeed()} mph"); // Output: My car's current speed is: 30 mph
            

Properties

Properties provide a flexible mechanism for reading, writing, or computing the value of a private field. They are often used to expose data in a controlled manner.

In the `Car` class example above, Make, Model, and Year are auto-implemented properties, while speed is a private field with a public method GetCurrentSpeed to access its value.

Constructors

Constructors are special methods used to initialize objects when they are created. They have the same name as the class and do not have a return type. If no constructor is defined, a default parameterless constructor is provided by the compiler.

Methods

Methods define the actions that an object can perform. They encapsulate a block of code that executes when the method is called.

Key .NET Class Concepts
  • Encapsulation: Bundling data (fields) and methods that operate on that data within a single unit (class).
  • Abstraction: Hiding complex implementation details and exposing only essential features.
  • Inheritance: Allowing a new class to inherit properties and methods from an existing class.
  • Polymorphism: The ability of an object to take on many forms, often through method overriding or interfaces.

Next Steps

Understanding classes and objects is crucial for building robust and maintainable applications in .NET. Continue to the next tutorial to explore how classes can inherit from one another.

Next: Inheritance