How is it that an enum derives from System.Enum and is an integer at the same time?

后端 未结 8 805
刺人心
刺人心 2021-01-30 16:51

Edit: Comments at bottom. Also, this.


Here\'s what\'s kind of confusing me. My understanding is that if I have an enum like this...



        
8条回答
  •  再見小時候
    2021-01-30 17:29

    Why not... it is perfectly valid, for example, for a structure to hold an int internally, and be convertible to int with an explicit cast operator... lets simulate an Enum:

    interface IEnum { }
    
    struct MyEnumS : IEnum
    {
        private int inner;
    
        public static explicit operator int(MyEnumS val)
        {
            return val.inner;
        }
    
        public static explicit operator MyEnumS(int val)
        {
            MyEnumS result;
            result.inner = val;
            return result;
        }
    
        public static readonly MyEnumS EnumItem1 = (MyEnumS)0;
        public static readonly MyEnumS EnumItem2 = (MyEnumS)2;
        public static readonly MyEnumS EnumItem3 = (MyEnumS)10;
    
        public override string ToString()
        {
            return inner == 0 ? "EnumItem1" :
                inner == 2 ? "EnumItem2" :
                inner == 10 ? "EnumItem3" :
                inner.ToString();
        }
    }
    

    This struct can be used quite the same way a struct can... of course, if you try to reflect the type, and call IsEnum property it will return false.

    Let's look at some usage comparison, with the equivalent enum:

    enum MyEnum
    {
        EnumItem1 = 0,
        EnumItem2 = 2,
        EnumItem3 = 10,
    }
    

    Comparing usages:

    Struct version:

    var val = MyEnum.EnumItem1;
    val = (MyEnum)50;
    val = 0;
    object obj = val;
    bool isE = obj is MyEnum;
    Enum en = val;
    

    Enum version:

    var valS = MyEnumS.EnumItem1;
    valS = (MyEnumS)50;
    //valS = 0; // cannot simulate this
    object objS = valS;
    bool isS = objS is MyEnumS;
    IEnum enS = valS;
    

    Some operations cannot be simulated, but this all shows what I intended to say... Enums are special, yes... how much special? not that much! =)

提交回复
热议问题