Enum “Inheritance”

后端 未结 15 1722
野性不改
野性不改 2020-11-22 07:21

I have an enum in a low level namespace. I\'d like to provide a class or enum in a mid level namespace that \"inherits\" the low level enum.

namespace low
{
         


        
15条回答
  •  半阙折子戏
    2020-11-22 08:08

    Ignoring the fact that base is a reserved word you cannot do inheritance of enum.

    The best thing you could do is something like that:

    public enum Baseenum
    {
       x, y, z
    }
    
    public enum Consume
    {
       x = Baseenum.x,
       y = Baseenum.y,
       z = Baseenum.z
    }
    
    public void Test()
    {
       Baseenum a = Baseenum.x;
       Consume newA = (Consume) a;
    
       if ((Int32) a == (Int32) newA)
       {
       MessageBox.Show(newA.ToString());
       }
    }
    

    Since they're all the same base type (ie: int) you could assign the value from an instance of one type to the other which a cast. Not ideal but it work.

提交回复
热议问题