Skip to content

Properties

Basic Properties

Properties use get, = (set), += (add), and -= (sub) accessors:

type Temperature
{
float _celsius;
float Fahrenheit
{
get this._celsius * 9.0 / 5.0 + 32.0;
=(v) this._celsius = (v - 32.0) * 5.0 / 9.0;
}
float Celsius
{
get this._celsius;
=(v) this._celsius = v;
}
}
Temperature t;
t.Celsius = 100.0;
Console.WriteLine($"{t.Fahrenheit}"); // 212.0

Expression Body vs Block Body

Accessors support both expression bodies and block bodies:

type Calc
{
int _val;
// Expression body accessor
int Double get this._val * 2;
// Block body accessor
int Value
{
get { return this._val; }
=(v) { this._val = v; }
}
}

Auto Properties and Backing Fields

Use get; and =; (with semicolons, no body) for auto-generated accessors. The compiler creates a hidden backing field named __prop_<PropertyName> to store the value:

type Person
{
string Name { get; =; }
int Age { get; =; }
}
Person p;
p.Name = "Alice"; // stores into __prop_Name
p.Age = 30; // stores into __prop_Age

A backing field is automatically created when any of these conditions are met:

  • An auto-accessor is used (get; or =;)
  • The prop keyword appears in any accessor or method body
  • A property initializer (= expr) is present after the closing brace

Read-only auto property (no setter):

type Config
{
int MaxSize { get; }
}

The prop Keyword

Inside a property body, prop is an alias for the auto-generated backing field (__prop_<PropertyName>). This lets you mix auto and custom accessors that share the same storage:

type Clamped
{
int Value
{
get; // auto-reads from __prop_Value
=(v)
{
if (v > 100) { prop = 100; } // writes to __prop_Value
else { prop = v; }
}
}
}

Using prop in a custom accessor triggers backing field creation even without an auto-accessor:

type Manual
{
int Data
{
get prop; // explicit getter using prop
=(v) prop = v; // explicit setter using prop
}
}

Property Initializers

Properties with backing fields can have default values. The initializer goes after the closing brace:

type Box
{
int Value { get; =; } = 25;
}
Box b;
Console.WriteLine($"{b.Value}"); // 25

Multiple properties with initializers work independently:

type Rect
{
int Width { get; =; } = 100;
int Height { get; =; } = 50;
float Temp { get; =; } = 36.6;
}

Compound Assignment Accessors

Define += (add) and -= (sub) accessors for custom compound assignment. If no += accessor is defined, obj.Prop += x falls back to obj.Prop = obj.Prop + x automatically.

type Counter
{
int _count;
int Count
{
get this._count;
=(v) this._count = v;
+=(v) this._count = this._count + v;
-=(v) this._count = this._count - v;
}
}
Counter c;
c.Count = 10;
c.Count += 5; // calls add accessor
c.Count -= 3; // calls sub accessor

Accessor Visibility

Individual accessors can have their own access modifier:

type Box
{
int Value
{
get; // Public (inherits default)
private =(v) prop = v; // Private setter
}
void Init(int v) { this.Value = v; } // OK: internal access
}

Computed Properties

Properties without backing fields — pure computation, no storage:

type Rect
{
int width;
int height;
int Area get this.width * this.height; // no backing field
}

Methods and Fields Inside Properties

Property bodies can contain nested methods and fields alongside accessors. These members are accessed via obj.Property.member, similar to groups:

Methods Inside Properties

type Counter
{
int Value
{
get;
=(v) prop = v;
void Reset() { prop = 0; } // Method using 'prop'
}
}
Counter c;
c.Value = 42;
c.Value.Reset(); // Calls method inside property
return c.Value; // 0

Methods inside property bodies can use prop to read and write the backing field.

Fields Inside Properties

type Tracker
{
int Value
{
get; =;
int writeCount; // Field inside property
}
}
Tracker t;
t.Value = 10;
t.Value.writeCount = 5;

Fields declared inside a property body are stored as regular type fields but are accessed through the property name, similar to groups.

Inline-Field Properties

A property whose get and = both target the same field collapse to direct field access at the use site — there is no method call. Reads of p.Age and writes to p.Age are inlined to the equivalent _age load/store. Property accessors that perform any other work fall back to the regular call form.

type Person
{
int _age;
int Age
{
get this._age;
=(v) this._age = v;
}
}

unsafe Accessors

A property accessor body can carry a trailing unsafe modifier, opening an unsafe context inside the accessor body — the same form regular function bodies accept (fn(...) unsafe { ... }):

type Buffer
{
int* _data;
int* Data
{
get unsafe { return this._data; }
=(v) unsafe this._data = v;
}
}

Both expression-body (get unsafe expr;) and block-body forms are accepted.

Trust Constraints

A property declaration accepts requires(caller: trusted) after the type — the constraint propagates to all accessors:

type System
{
int _flags;
int Flags requires(caller: trusted)
{
get this._flags;
=(v) this._flags = v;
}
}