Quantcast
Viewing latest article 5
Browse Latest Browse All 62

Answer by Kyle McClellan for How to deal with nullable reference types with System.Text.Json?

Another option, for those who want to handle missing properties with meaningful exceptions:

using System;public class Car{    private string? name;    private int? year;    public string Name    {        get => this.name ?? throw new InvalidOperationException($"{nameof(this.Name)} was not set.");        set => this.name = value;    }    public int Year    {        get => this.year ?? throw new InvalidOperationException($"{nameof(this.Year)} was not set.");        set => this.year = value;    }}

Viewing latest article 5
Browse Latest Browse All 62

Trending Articles