Properties and Groups
Properties
Properties provide controlled access to a type’s data through getter and setter accessors. They look like fields from the outside but execute code on read/write.
Basic Syntax
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.0Accessors use get for reading, = for assignment, and += / -= for compound assignment.
Expression Body vs Block Body
type Calc{ int _val;
// Expression body — single expression, no braces int Double get this._val * 2;
// Block body — multiple statements 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_Namep.Age = 30; // stores into __prop_AgeA backing field is created automatically when any of these conditions are met:
- An auto-accessor is used (
get;or=;) - The
propkeyword appears in any accessor or method body - A property initializer (
= expr) is present after the closing brace
Read-only auto property (getter only, no setter):
type Config{ int MaxSize { get; }}The prop Keyword
Inside a property body, prop is an alias for the backing field. 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; float Temp { get; =; } = 36.6;}
Box b;Console.WriteLine($"{b.Value}"); // 25Compound Assignment Accessors
Define += and -= accessors for custom compound assignment logic. If not 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 += accessorc.Count -= 3; // calls -= accessorAccessor Visibility
Individual accessors can have their own access modifier:
type Box{ int Value { get; // public (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 with 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:
type Counter{ int Value { get; =(v) prop = v; void Reset() { prop = 0; } // method using backing field }}
Counter c;c.Value = 42;c.Value.Reset(); // Value is now 0type Tracker{ int Value { get; =; int writeCount; // field inside property }}
Tracker t;t.Value = 10;t.Value.writeCount = 5;Groups
Groups provide organizational namespacing inside a type. They have zero runtime cost — fields are stored flat in the parent type. Members inside a group are accessed exclusively through the group name.
Use groups when you want to organize related fields and methods under a named prefix without any accessor logic. Properties are for controlled access (getters/setters); groups are for structure.
Basic Syntax
type Player{ int id;
group Stats { int health; int mana;
int Total() { return this.Stats.health + this.Stats.mana; } }}
Player p;p.Stats.health = 100;p.Stats.mana = 50;Console.WriteLine($"{p.Stats.Total()}"); // 150
// p.health = 100; // ERROR: must access through groupAccess Inside Methods
Inside group methods, use this.GroupName.member. Group methods can also access ungrouped fields:
type Player{ int id; group Stats { int health; int Combined() { return this.id + this.Stats.health; } }}Nested Groups
Groups can contain other groups:
type Game{ group Physics { group Collision { int layer; } }}
Game g;g.Physics.Collision.layer = 3;Access Modifiers
A group-level modifier restricts access to the entire group:
type Widget{ private group Internal { int state; }
void Init() { this.Internal.state = 42; } int GetState() { return this.Internal.state; }}
Widget w;w.Init();w.GetState(); // OK// w.Internal.state; // ERROR: Internal is privateProperties Inside Groups
Groups can contain properties and operator overloads:
type Config{ group Settings { int _volume; int Volume { get this.Settings._volume; =(v) this.Settings._volume = v; } }}
Config c;c.Settings.Volume = 75;Properties vs Groups
| Properties | Groups | |
|---|---|---|
| Purpose | Controlled access (get/set logic) | Organizational namespacing |
| Runtime cost | Accessor calls (may inline) | Zero — fields stored flat |
| Backing storage | Auto-generated __prop_* field or explicit field | Fields declared directly |
| Access syntax | obj.Prop (looks like a field) | obj.Group.member |
| Contains | Accessors, methods, fields | Fields, methods, properties, nested groups |
| Use when | You need validation, computation, or encapsulation | You want to organize related members under a name |