Skip to content
Codescope

Properties, fields & auto-properties

What the compiler generates behind { get; set; }.

intro7 min6 steps

Before you start

In C#, a **field** is raw storage — a slot in the object. A **property** looks the same from outside (obj.Name = "Ana") but is actually a pair of methods (get_Name and set_Name) wrapping the field.

Why the layer? Because "read this value" and "write this value" often want extra behaviour: validation, change notification, computed values. Properties let you add that behaviour later without changing any calling code.

This lesson shows what the compiler generates behind { get; set; }, and covers the modern variants: auto-properties, init-only, and computed properties.

Heap

fields are the actual storage; properties are the API
Person
_name
null
private field — raw storage

A field is just a slot in the object. No logic, no protection.

Step 1 of 6. A field is raw storage. `private string _name` allocates space inside the object for a string reference. It is the actual data — nothing runs when you read or write it. Fields are usually `private` because exposing them directly means any code can write any value, with no validation or notification.

01 / 06

Step 01 / 06

A field is raw storage

private string _name allocates space inside the object for a string reference. It is the actual data — nothing runs when you read or write it.

Fields are usually private because exposing them directly means any code can write any value, with no validation or notification.

properties-and-fields.csC#
public class Person {    // Field — raw storage    private string _name;    // Property — controlled access    public string Name {        get { return _name; }        set { _name = value; }    }    // Auto-property — compiler generates the backing field    public int Age { get; set; }    // Init-only — settable in constructor or initialiser, then frozen    public string Id { get; init; }    // Computed — no storage, derived each time    public bool IsAdult => Age >= 18;}// Usagevar p = new Person { Name = "Ana", Age = 25, Id = "A001" };p.Name = "Ben";   // calls the set accessorp.Age  = 30;      // calls the auto-generated set// p.Id = "X";    // error: init-only after construction