我如何比较两个枚举?

c#

这是我的代码

[Flags]
public enum Colors { None = 0, Red = 1, Yellow = 2, Blue = 4, Green = 8, Orange = 16, Brown = 32, Cyan = 64, Magenta = 128, Other = 256 };



class Program
{
    Colors familyRGB = Colors.Red | Colors.Blue | Colors.Green;
    Colors familyCMY = Colors.Cyan | Colors.Magenta | Colors.Yellow;
    Colors familyRYB = Colors.Red | Colors.Blue | Colors.Yellow;

我必须编写一个方法,该方法以两个家庭为参数并且执行的操作很少,所以我这样做了

public static void TwoFamilyColorSystem(Colors family1, Colors family2)

我想打印系列 1 中存在但系列 2 中不存在的元素。我该怎么做?例如,如果我使用 RGB 和 RYB,它应该打印绿色,因为绿色存在于 RGB 而不是 RYB。

回答

~反转位。所以:

return (Colors)(family1 & ~family2);


以上是我如何比较两个枚举?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>