Can you add to an enum type in run-time

后端 未结 6 645
甜味超标
甜味超标 2020-12-08 07:06

If I have an enum type:

public enum Sport
{
    Tennis = 0;
    Football = 1;
    Squash = 2;
    Volleyball = 3;
}

Can I somehow add durin

6条回答
  •  庸人自扰
    2020-12-08 07:30

    I would create a complete Enum, with all the values you will ever need like this

    Public enum Sport
    {
        Tennis = 0;
        Football = 1;
        Squash = 2;
        Volleyball = 3;
        PingPong = 4;
        Rugby = 5;  // for example
    }
    

    And then keep a LIST of Invalid Sport entries, so the list would initially contain PingPong and Rugby. Every time you access the Enum, also check with your Invalid Sports list.

    Then you can simply adjust your Invalid Sports List to suit, at any stage

提交回复
热议问题