C#C
C#2y ago
SpReeD

The best-practice way to use a plausibility check

So, I have the following class

namespace CleanLib.Lego.Parts;

public class Color : Entity {
    private string? _Name;
    public string Name {
        get => this._Name!;
        set {
            value.ThrowIfNullOrWhiteSpace();

            this._Name = value;
        }
    }

    private string? _Code;
    public string Code {
        get => this._Code!;
        set {
            value.ThrowIfNullOrWhiteSpace();

            this._Code = value;
        }
    }

    public Color(string name, string code) {
        this.Name = name;
        this.Code = code;
    }
}


Is it okay to throw in the setter of the properties or is it somehow "better" to check this in the constructor?
Logically spoken, both values won't ever change, but may be "corrected" if inserted wrongly in the database.
Was this page helpful?