假設我需要在標誌和非標誌變體中都使用枚舉。枚舉的標誌和非標誌變體
選項1:我可以複製的一切:
enum Color { Red, Blue, Green } [Flags] enum Colors { None = 0, Red = 1, Blue = 2, Green = 4 } // use cases Color currentColor; Colors supportedColors;
選項2:我可以只使用標誌變異的一切:
Colors currentColor; // ugly, since neither "None" nor "Red | Blue" should be valid
我不像以下任何一種:在選項1中,Color.Red
和Colors.Red
完全不相關,這可能需要禁止綁定代碼。另外,我必須保持兩個枚舉同步。選項2的缺點是顯而易見的。我實際上喜歡的是類似於
enum Colors = Flag set of Color;
是否有更優雅的解決方案來滿足此要求?
您可以請擴展用例嗎?你是否曾經需要比較'supportedColors'和'currentColor'? – Oded
@Oded:是的,能夠做一些類似'bool currentColorIsSupported =(currentColor And supportedColor <> 0)'是很棒的事情。 – Heinzi