Auto implemented properties
Context: Auto‑implemented properties simplify syntax by letting the compiler generate a hidden backing field.
Syntax
Section titled “Syntax”public string Name { get; set; }public int Age { get; set; } = 18; // with defaultRead‑Only Auto Property
Section titled “Read‑Only Auto Property”public string Id { get; } // can be set only in constructorInit‑Only Auto Property
Section titled “Init‑Only Auto Property”public DateTime CreatedAt { get; init; }Example
Section titled “Example”public class Product{ public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; }}
// Usagevar product = new Product { Id = 1, Name = "Laptop", Price = 999.99M };The compiler generates a private field like _<PropertyName>k__BackingField.