C# VS2026
Lesson 16 of 30

Object-Oriented Programming – Classes and Objects

C# in Visual Studio 2026 — a hands-on guide for developers at every level.

Classes and Objects

A class is a blueprint. An object is an instance of that blueprint created with new.

public class BankAccount
{
    public string Owner   { get; set; }
    public decimal Balance { get; private set; }

    public BankAccount(string owner, decimal initialBalance)
    {
        Owner   = owner;
        Balance = initialBalance;
    }

    public void Deposit(decimal amount)
    {
        if (amount <= 0) throw new ArgumentException("Amount must be positive");
        Balance += amount;
    }

    public void Withdraw(decimal amount)
    {
        if (amount > Balance) throw new InvalidOperationException("Insufficient funds");
        Balance -= amount;
    }

    public override string ToString()
        => $"{Owner}: {Balance:C}";
}

// Usage
var acc = new BankAccount("Alice", 1000m);
acc.Deposit(500m);
acc.Withdraw(200m);
Console.WriteLine(acc);  // Alice: $1,300.00

Records (C# 9+)

Records are immutable reference types perfect for data objects:

public record Point(double X, double Y);

var p1 = new Point(1.0, 2.0);
var p2 = p1 with { Y = 5.0 };  // non-destructive copy
Console.WriteLine(p1 == p2);   // False

Static Members

public class Counter
{
    public static int Total { get; private set; }
    public Counter() => Total++;
}

var _ = new Counter();
var __ = new Counter();
Console.WriteLine(Counter.Total);  // 2