SmartEnum
SmartEnum copied to clipboard
Are you open to a PR for a nullable `Name`?
hey steve, wanted to check if you were open to a PR that would allow for a null name? this could be useful when you want a smart enum to capture a response like not given without jumping through extra hoops externally or adding an empty string in the smart enum. for example:
public abstract class KnownRace(string name, int value) : SmartEnum<KnownRace>(name, value)
{
public static readonly KnownRace White = new WhiteType();
public static readonly KnownRace BlackOrAfricanAmerican = new BlackOrAfricanAmericanType();
public static readonly KnownRace NotGiven = new NotGivenType();
public abstract Guid? LegacyGuid { get; }
private const string legacyOtherGuid = "f5b897e4-cdb7-471b-a6d0-f73d399538b2";
private class WhiteType() : KnownRace("White", 0)
{
public override Guid? LegacyGuid => Guid.Parse("113c8858-d971-4335-909d-48e6808d8839");
}
private class BlackOrAfricanAmericanType() : KnownRace("Black or African American", 1)
{
public override Guid? LegacyGuid => Guid.Parse("ffbb2f4c-9667-40df-bac4-14ab568023a4");
}
//....
private class NotGivenType() : KnownRace(" ", 100)
{
public override Guid? LegacyGuid => null;
}
}
I dunno - I don't think I'd want to use a null for that. Why wouldn't you use a string like "Not Given" which is more clear and less likely to cause runtime NullReference exceptions?
Makes it harder for something like a dropdown where these options would be. If they select nothing, that still gets recognized as an option and can have extension classes off of it.
You could do a 'not given' name instead of that, but it feeds into the gymnastics outside of this workflow. Where you always have to check for that and return nothing, maybe not show that also as on a screen if the business doesn't want that, etc.