Property Patterns in C#

From the C# Programming Language cheat sheet · Properties and Indexers · verified Jul 2026

Property Patterns

Auto-properties, computed properties, and validation

csharp
// Auto-properties
public string Name { get; set; }
public int Age { get; private set; }

// Read-only property
public string Id { get; } = Guid.NewGuid().ToString();

// Computed property
public string FullName => $"{FirstName} {LastName}";

// Property with validation
private int _age;
public int Age
{
    get => _age;
    set => _age = value >= 0 ? value : 0;
}
💡 Init-only properties can only be set during object initialization
⚡ Computed properties recalculate on each access
📌 Property setters can include validation logic
🟢 Indexers allow array-like access to custom classes
Back to the full C# Programming Language cheat sheet