LoginSignup
0
2

More than 5 years have passed since last update.

C#の列挙体

Posted at

拡張メソッドとかLLっぽくて、なかなか楽しい。

class Program
{
    static void Main(string[] args)
    {
        foreach(EnumNumper e in Enum.GetValues(typeof(EnumNumper)))
        {
            Debug.WriteLine("{0}={1} {2}", e.ToString(), e.ToInt(), e.HasFlg(1));
        }
    }
}

public enum EnumNumper
{
    Zero = 0,
    One = 1,
    Two = 2,
    Three = 4,
    Four = 8,
    Five = 16
}

public static class EnuNumberClass
{
    public static int ToInt(this EnumNumper e)
    {
        return (int)e;
    }
    public static bool HasFlg(this EnumNumper e, int x)
    {
        return ((int)e & x) == 0 ? false : true;
    }
}

実行するとこんなかんじ。

A=0 False
B=1 True
C=2 False
D=4 False
E=8 False
F=16 False
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2