← C# tutorial

C#

Classes & Objects

A class describes the fields and methods its instances will have. new ClassName(...) creates an object from that blueprint, running the class's constructor to set up its initial data.

Example: a Student class

using System;

class Student {
    public string Name;
    public string Track;

    public Student(string name, string track) {
        Name = name;
        Track = track;
    }
}

class Program {
    static void Main() {
        Student ada = new Student("Ada", "Frontend");
        Console.WriteLine($"{ada.Name} is studying {ada.Track}");
    }
}
Ada is studying Frontend

public before a field means it's accessible from outside the class — without it, C# fields default to private, accessible only from inside the class itself. This is a real, deliberate design choice (called encapsulation) that comes up across almost every object-oriented language, controlling exactly what outside code is allowed to see and touch directly versus what stays as an internal implementation detail.

Example
using System;

class Student {
    public string Name;
    public string Track;

    public Student(string name, string track) {
        Name = name;
        Track = track;
    }
}

class Program {
    static void Main() {
        Student ada = new Student("Ada", "Frontend");
        Console.WriteLine($"{ada.Name} is studying {ada.Track}");
    }
}
Output
Ada is studying Frontend