Roadmap › OOP Concepts in C# › Module: Classes and Objects
Lessons in this course (15)

1. Class, object, fields, properties and methods

Basic★ Must knowLesson 1 of 15

A class is a blueprint; an object is an instance created from it at runtime.

csharp
public class BankAccount
{
    private decimal _balance;                       // field (state) — private
    public string Owner { get; }                    // read-only property
    public decimal Balance => _balance;             // computed property

    public BankAccount(string owner, decimal opening = 0)   // constructor
    {
        Owner = owner;
        _balance = opening;
    }

    public void Deposit(decimal amount)             // method (behaviour)
    {
        if (amount <= 0) throw new ArgumentOutOfRangeException(nameof(amount));
        _balance += amount;
    }
}

var acc = new BankAccount("Divakar", 500);           // object
acc.Deposit(250);
Console.WriteLine(acc.Balance);                      // 750