© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
2 replies
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;
    }
}
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Tests best practice
C#CC# / help
3y ago
Thread signaling still the best practice?
C#CC# / help
3y ago
❔ Best way to use smtp to send emails
C#CC# / help
3y ago
Best practice to eliminate switch statements?
C#CC# / help
2y ago