← 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
Ready to go further?

These free tutorials are a preview of what a Code Secure Academy cohort covers, project work and all.

Explore our tracks

Cookie preferences

Choose what you're comfortable with. You can change this any time from the footer.

Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account