Specify allowed enum values in a property

后端 未结 4 1843
一个人的身影
一个人的身影 2021-01-11 17:24

Is it possible to specify that a enum property can only have a range of values?

enum Type
{
    None,
    One,
    Two,
    Three
}

class Object
{
    [Allo         


        
4条回答
  •  [愿得一人]
    2021-01-11 17:46

    You could do the validation in setter logic.

    EDIT: some example:

    class Object
    {
        private Type _value;
    
        public Type objType{ 
    
            get{ return _value; }
            set{
                if(value != Type.One && value != Type.Three)
                    throw new ArgumentOutOfRangeException();
                else
                    _value = value;
            }
        }
    }
    

提交回复
热议问题