C# Reference ยท Classes and Object-Oriented Concepts
Properties
Reference for C# properties: auto-properties, get/set/init accessors, computed and expression-bodied properties, required, the field keyword and partial properties.
A property looks like a field to the caller but is implemented by one or two accessor methods: get returns the value and set (or init) stores it. Properties let a type keep its data private while still offering a simple public surface, and they allow validation, computation and change notification without changing the calling code.
The simplest form is an auto-implemented property: you write only the accessors and the compiler creates a hidden backing field. When you need logic in an accessor, write the body yourself, and in C# 14 you can refer to the compiler-generated field with the field keyword.
public class Person { public string Name { get; set; } = ""; // auto-property public int Age { get; init; } // set only during initialization public bool IsAdult => Age >= 18; // computed, read-only }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public int A { get; set; } // read-write public int B { get; } // get-only: set in constructor or initializer public int C { get; init; } // set in constructor or object initializer only public int D { get; private set; } // public read, private write public int E => A * 2; // expression-bodied, read-only public required string F { get; init; } // callers must set it public int G { get; set => field = Math.Max(0, value); } // C# 14 field keyword
| Part | Description |
|---|---|
get |
Returns the value. A property with only get is read-only. |
set |
Stores value. Can have its own, stricter access modifier (private set, protected set). |
init |
Like set, but allowed only in constructors, object initializers and with expressions. Makes immutable objects friendly to initializers. |
required (C# 11) |
The compiler forces callers to set the member in an object initializer. |
field (C# 14) |
Refers to the automatically generated backing field inside accessors. |
| Partial property (C# 13) | A declaring part and an implementing part, used mostly by source generators. |
Usage notes
- Properties are methods. They can run arbitrary code, throw, and be
virtual,abstractor declared in an interface. Keep getters cheap and free of side effects; callers assume they behave like fields. - Not a field: you cannot pass a property by
reforout, and a property of a struct returned from a getter is a copy, soobj.Point.X = 1does not compile for a struct-typed property. initversusprivate set:initallows setting from outside during construction (object initializers),private setallows changes only from inside the class at any time.- Auto-property initializers:
public List<int> Items { get; } = new();runs before the constructor body. - Computed properties (
=> expression) have no backing storage. A get-only property assigned in a constructor does. - Before C# 14, a validating setter needed an explicit backing field. Existing code using
_namefields remains valid. - Naming: public properties use PascalCase; the backing field is
_camelCaseby common convention. - Data binding and serializers (JSON, ORMs, UI frameworks) work with properties, not fields. That is a practical reason to expose data as properties.
Related
- Classes and objects, Indexers, Constructors, Immutable types
- Lessons: Properties, Classes, C# 14