diff --git a/docs/csharp/whats-new/csharp-14.md b/docs/csharp/whats-new/csharp-14.md index 5a4261fd6edfa..976e56b70ab80 100644 --- a/docs/csharp/whats-new/csharp-14.md +++ b/docs/csharp/whats-new/csharp-14.md @@ -90,6 +90,31 @@ public string Message get; set => field = value ?? throw new ArgumentNullException(nameof(value)); } +``` +### Common patterns with `field` + +**Lazy Initialization:** +```csharp +public SqlConnection Connection +{ + get => field ??= new SqlConnection("connection-string"); +} +public string DisplayName +{ + get; + set + { + if (field == value) return; + field = value; + OnPropertyChanged(); + } +} +public string Email +{ + get; + set => field = value?.Trim()?.ToLowerInvariant() ?? string.Empty; +} + ``` You can declare a body for one or both accessors for a field backed property.