Object-Oriented C#

Classes and Objects

Model state and behavior together in a C# class.

Introduction

A class defines a reusable reference type; each object has its own state.

The constructor establishes valid initial state and the method changes state through a controlled operation.

Runnable C# example

C#
Run code →
Program.cs
using System;

class Counter
{
    public int Value { get; private set; }
    public void Increment() => Value++;
}

class Program
{
    static void Main()
    {
        var counter = new Counter();
        counter.Increment();
        counter.Increment();
        Console.WriteLine(counter.Value);
    }
}
Result of running the code
2

Practice with the debugger

Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges