Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/csharp/whats-new/csharp-14.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ public string Message
get;
set => field = value ?? throw new ArgumentNullException(nameof(value));
}
```
### Common patterns with `field`

**Lazy Initialization:**
```csharp
Comment thread
gidadomukhtar marked this conversation as resolved.
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.
Expand Down
Loading