AnyCompiler

Properties

Unlike Java (which uses plain getter/setter methods by convention) or Python (which uses a @property decorator), C# has properties as a real language feature. { get; set; } with no body — an auto-property — generates a hidden backing field for you automatically, and you use it exactly like a public field from the outside.

using System;

class Person {
    public string Name { get; set; }
    public int Age { get; set; }
}

class Program {
    static void Main() {
        var p = new Person();
        p.Name = "Alice";
        p.Age = 30;
        Console.WriteLine(p.Name + " is " + p.Age);
    }
}
Output
Alice is 30

Full properties with real logic

A property can also have a real body for its get and set, backed by your own private field — useful when a property needs to compute its value or validate what's assigned to it. value inside a set block is the implicit name for whatever was assigned.

using System;

class Temperature {
    private double celsius;
    public double Celsius {
        get { return celsius; }
        set { celsius = value; }
    }
    public double Fahrenheit {
        get { return celsius * 9.0 / 5.0 + 32.0; }
    }
}

class Program {
    static void Main() {
        var t = new Temperature();
        t.Celsius = 100;
        Console.WriteLine(t.Fahrenheit);
    }
}
Output
212

Fahrenheit here has no set at all — it's a computed, read-only property. Next: encapsulation — using a property's access modifiers to protect a class's own invariants, and the real compiler error you get from breaking that promise.

Practice exercise

Define a class Person with auto-properties Name (string) and Age (int). Create a Person, set Name to "Alice" and Age to 30, then print both.