Lesson 6 Β· ~14 min Β· ← Lesson 5

Classes & objects: data with behavior

A class is a blueprint; an object is a thing built from it. Variables of a class type hold a reference to that thing. Three ideas: fields hold the data, constructors set it up, methods act on it.

A class with everything on it

class BankAccount
{
    public string Owner;                 // field: each object gets its own

    public BankAccount(string owner)     // constructor: runs at 'new'
    {
        Owner = owner;
    }

    public void Print()
    {
        Console.WriteLine(Owner);
    }
}

BankAccount acct = new BankAccount("Ada");   // build it
acct.Print();

Piece by piece: public = visible from outside the class. The constructor has the same name as the class and no return type β€” its job is to make the object valid the moment it exists. new allocates and runs it.

Each object is its own thing

class Dog
{
    public string Name;

    public Dog(string name) { Name = name; }
}

Dog a = new Dog("Ada");
Dog b = new Dog("Grace");
Console.WriteLine(a.Name);
Console.WriteLine(b.Name);        // two objects, two Names β€” no sharing

static: belongs to the CLASS, not to an object

class Counter
{
    public static int Total = 0;      // ONE copy, shared by everything

    public static void Increment()
    {
        Total++;
    }
}

Counter.Increment();                 // called on the class β€” no object needed
Counter.Increment();
Counter.Increment();
Console.WriteLine(Counter.Total);

You already use static members constantly: Console.WriteLine, int.Parse, Math.Max β€” all methods on a class, no object involved. Rule of thumb: static for utilities and shared counters, instance for "each one has its own."

Properties: fields with a public face

The modern C# style β€” auto-properties look like fields but are proper members with get/set:

class Rectangle
{
    public double Width { get; set; }     // auto-property
    public double Height { get; set; }

    public double Area()
    {
        return Width * Height;            // no 'this.' needed β€” it's implied
    }
}

Rectangle r = new Rectangle { Width = 3, Height = 4 };   // object initializer
Console.WriteLine(r.Area());

The object initializer (new Rectangle { … }) sets properties right after construction β€” no parameter constructor needed for simple setup.

this: when names collide

Inside instance code, this is "the current object." Its main job: disambiguate a parameter from a field of the same name:

class Dog
{
    private string name;

    public Dog(string name)
    {
        this.name = name;   // this.name = the field; name = the parameter
    }
}

Style tip: naming fields in PascalCase (Name) or prefixing (_name) makes this. almost never necessary β€” pick one convention per codebase.

Constructors gone wrong

Once you declare ANY constructor, C# stops providing the free empty one β€” and new Person() fails with CS1729. The fix is either pass the arguments, or add an explicit parameterless constructor.

Practice this now