C#/수업 내용

[C#] enum사용

JSH1 2021. 8. 17. 15:19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        enum STATE
        {
            Idle = 100,
            Walk = 200,
            Run,
            Attack,
            Hit,
            Die
        }
        
        static void Main(string[] args)
        {
            //변수의 정의
            //변수타입 변수명
            STATE state;

            //변수에 값을 할당
            state = STATE.Idle;
            Console.WriteLine("state: {0}", state);

            //열거형식을 정수로 변환
            int a = Convert.ToInt32(state);
            Console.WriteLine("a: {0}", a);

            //정수를 열거형식으로 변환
            STATE state2 = (STATE)200;
            Console.WriteLine("state2: {0}", state2);
            
            //없는 상수라면 정수로 나온다
            STATE state3 = (STATE)500;
            Console.WriteLine("state3: {0}", state3);
        }
    }
}

'C# > 수업 내용' 카테고리의 다른 글

[C#] ConsoleKeyInfo, 키 입력  (0) 2021.08.18
2021-08-18 오전  (0) 2021.08.18
2021-08-17 2  (0) 2021.08.17
2021-08-17  (0) 2021.08.17
2021-08-16  (0) 2021.08.16