C# Tutorial: Properties and Value Semantics
Fields, auto-properties, computed properties, init-only setters, and value vs reference semantics in C#.
Fields vs Properties
**Fields** are raw data; **properties** wrap them with `get`/`set` logic:
```csharp
class Person {
// auto-property: compiler generates the backing field
public string Name { get; set; } = "";
public int Age { get; set; }
// computed property — no backing field
public bool IsAdult => Age >= 18;
}
var p = new Person { Name = "Alice", Age = 25 };
Console.WriteLine(p.IsAdult); // true
```
---
init-Only Properties (C# 9+)
Set once during object initialisation:
```csharp
class Point {
public int X { get; init; }
public int Y { get; init; }
}
var pt = new Point { X = 3, Y = 4 };
// pt.X = 5; ← compile error after construction
```
---
Required Properties (C# 11+)
```csharp
class User {
public required string Email { get; set; }
}
// var u = new User(); ← compile error if Email not set
var u = new User { Email = "a@b.com" };
```
---
Backing Field with Validation
```csharp
class BankAccount {
private decimal _balance;
public decimal Balance {
get => _balance;
set {
if (value < 0) throw new ArgumentOutOfRangeException(nameof(value));
_balance = value;
}
}
}
```
---
Value vs Reference Semantics
**struct** = value type (copied on assignment):
```csharp
struct Vector2 { public float X, Y; }
Vector2 a = new Vector2 { X = 1, Y = 2 };
Vector2 b = a; // copy
b.X = 99;
Console.WriteLine(a.X); // still 1
```
**class** = reference type (shared on assignment):
```csharp
class Box { public int Value; }
Box a = new Box { Value = 1 };
Box b = a; // same object
b.Value = 99;
Console.WriteLine(a.Value); // 99!
```
---
records — Immutable Value Objects (C# 9+)
```csharp
record Product(string Name, decimal Price);
var p1 = new Product("Book", 9.99m);
var p2 = p1 with { Price = 12.99m }; // non-destructive mutation
Console.WriteLine(p1.Price); // 9.99
Console.WriteLine(p2.Price); // 12.99
```
---
What's Next?
You understand C# data modeling primitives. Next: **structs and classes** — constructors, inheritance, encapsulation.
What you'll learn in this C# properties and value semantics tutorial
This interactive C# tutorial has 5 hands-on exercises. Estimated time: 12 minutes.
- Auto-Properties — Define a `Person` class with auto-properties `Name` (string) and `Age` (int). Create an instance with `Name = "Alice"` a…
- Computed Property — Add a computed property `IsAdult` to `Person` that returns `true` when `Age >= 18`. Print `"Adult: True"` for a person a…
- init-Only Properties — Define a `Point` record-like class with `init`-only properties `X` and `Y`. Create a point at `(3, 4)` and print `"3, 4"…
- Value vs Reference — Demonstrate that structs are value types: copy a `struct Counter { public int Value; }` into `b`, change `b.Value`, and …
- record — Non-Destructive Mutation — Define `record Product(string Name, decimal Price)`. Create `p1` with `Name = "Book"` and `Price = 9.99m`. Create `p2` u…
C# Properties and Value Semantics concepts covered
- Fields vs Properties
- init-Only Properties (C# 9+)
- Required Properties (C# 11+)
- Backing Field with Validation
- Value vs Reference Semantics
- records — Immutable Value Objects (C# 9+)
- What's Next?