Why is a Nullable not a valid Custom Attribute Parameter when T is?

前端 未结 4 2124
猫巷女王i
猫巷女王i 2020-12-30 22:07

If I have an enum like this

public enum Hungry
{
    Somewhat,
    Very,
    CouldEatMySocks
}

and a custom attribute like this

<         


        
4条回答
  •  旧时难觅i
    2020-12-30 22:46

    Instead of creating nullable enum, you can create default value for that enum. Enum pick default from 1st value, so set your enum like this

    public enum Hungry
    {
        None,
        Somewhat,
        Very,
        CouldEatMySocks
    }
    

    in your code you could do this to check for null

    if(default(Hungry) == HungerLevel)//no value has been set
    

提交回复
热议问题