Enum Bit Flags - Sample(Uses)

CSharp · Console

/Program.cs

using System;
					
/// <summary>
/// 권한3 옵션
/// </summary>
[Flags]
public enum Auth3Type
{
    None = 0

    , Opt0 = 1 << 0
    , Opt1 = 1 << 1
    , Opt2 = 1 << 2
    , Opt3 = 1 << 3
    , Opt4 = 1 << 4
    , Opt5 = 1 << 5
    , Opt6 = 1 << 6

    , OptAll = int.MaxValue
}

public class Program
{
	public static void Main()
	{
		Auth3Type typeAuth3 = Auth3Type.None;
		
		Console.WriteLine("----- Uses ----");
            
		Console.WriteLine("//모든 값 빼기");
		typeAuth3 = Auth3Type.None;
		Console.WriteLine("typeAuth3 = Auth3Type.None; ====> : " + typeAuth3);
		Console.WriteLine("typeAuth3.HasFlag(Auth3Type.Opt1) ====> : " + typeAuth3.HasFlag(Auth3Type.Opt1));
		Console.WriteLine("typeAuth3.HasFlag(Auth3Type.Opt2) ====> : " + typeAuth3.HasFlag(Auth3Type.Opt2));
		Console.WriteLine("  ");


		Console.WriteLine("//모든 값 넣기");
		typeAuth3 = Auth3Type.OptAll;
		Console.WriteLine("typeAuth3 = Auth3Type.OptAll; ====> : " + typeAuth3);
		Console.WriteLine("typeAuth3.HasFlag(Auth3Type.Opt1) ====> : " + typeAuth3.HasFlag(Auth3Type.Opt1));
		Console.WriteLine("typeAuth3.HasFlag(Auth3Type.Opt2) ====> : " + typeAuth3.HasFlag(Auth3Type.Opt2));
		Console.WriteLine("  ");


		Console.WriteLine("//값 넣기");
		typeAuth3 = Auth3Type.Opt1 | Auth3Type.Opt3;
		Console.WriteLine("typeAuth3 = Auth3Type.Opt1 | Auth3Type.Opt3; ====> : " + typeAuth3);
		Console.WriteLine("  ");


		Console.WriteLine("//기존 값에 추가하기");
		typeAuth3 |= Auth3Type.Opt5 | Auth3Type.Opt6;
		Console.WriteLine("typeAuth3 |= Auth3Type.Opt5; ====> : " + typeAuth3);
		Console.WriteLine("  ");


		Console.WriteLine("//값 빼기");
		typeAuth3 &= ~Auth3Type.Opt5;
		Console.WriteLine("typeAuth3 &= ~Auth3Type.Opt5; ====> : " + typeAuth3);
		Console.WriteLine("  ");


		Console.WriteLine("//값 반전(있으면 빠지고 없으면 추가됨)");
		typeAuth3 ^= Auth3Type.Opt6;
		Console.WriteLine("typeAuth3 ^= Auth3Type.Opt6; ====> : " + typeAuth3);
		typeAuth3 ^= Auth3Type.Opt6;
		Console.WriteLine("typeAuth3 ^= Auth3Type.Opt6; ====> : " + typeAuth3);
		Console.WriteLine("  ");

		Console.WriteLine("//몇가지 값을 빼고 전체 설정 넣기");
		typeAuth3 = Auth3Type.OptAll ^Auth3Type.Opt1 ^Auth3Type.Opt6;
		Console.WriteLine("typeAuth3 = Auth3Type.OptAll ^Auth3Type.Opt1 ^Auth3Type.Opt6; ====> : " + typeAuth3);
		Console.WriteLine("  ");


		Console.WriteLine("//값 특정값이 있는지 확인");
		Console.WriteLine("typeAuth3.HasFlag(Auth3Type.Opt1) ====> : " + typeAuth3.HasFlag(Auth3Type.Opt1));
		Console.WriteLine("typeAuth3.HasFlag(Auth3Type.Opt2) ====> : " + typeAuth3.HasFlag(Auth3Type.Opt2));
		Console.WriteLine("  ");
	}
}
;