첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ Enum 클래스의 HasFlag 메소드를 사용해 플래그 조합에서 플래그 설정 여부를 구하는 방법을 보여준다.

TestProject.zip
0.00MB

▶ PetType.cs

namespace TestProject
{
    /// <summary>
    /// 애완동물 타입
    /// </summary>
    [Flags]
    public enum PetType
    {
        /// <summary>
        /// 없음
        /// </summary>
        None = 0,

        /// <summary>
        /// 개
        /// </summary>
        Dog = 1,

        /// <summary>
        /// 고양이
        /// </summary>
        Cat = 2,

        /// <summary>
        /// 새
        /// </summary>
        Bird = 4,

        /// <summary>
        /// 토끼
        /// </summary>
        Rabbit = 8,

        /// <summary>
        /// 기타
        /// </summary>
        Other = 16
    }
}

 

▶ Program.cs

namespace TestProject;

/// <summary>
/// 프로그램
/// </summary>
class Program
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Private

    #region 프로그램 시작하기 - Main()

    /// <summary>
    /// 프로그램 시작하기
    /// </summary>
    private static void Main()
    {
        PetType[] petTypeArray =
        {
            PetType.None,
            PetType.Dog | PetType.Cat,
            PetType.Dog
        };

        int noneCount = 0;
        int dogCount  = 0;

        foreach(PetType petType in petTypeArray)
        {
            if(petType.Equals(PetType.None))
            {
                noneCount++;
            }
            else if(petType.HasFlag(PetType.Dog))
            {
                dogCount++;
            }
        }

        Console.WriteLine("애완동물이 없는 경우 : {0}/{1}", noneCount, petTypeArray.Length);
        Console.WriteLine("개가 있는 경우       : {0}/{1}", dogCount , petTypeArray.Length);
    }

    #endregion
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요