본문 바로가기
Dev/C#

C# 기초 - enum, 구조체 struct

by 석맨.class 2024. 12. 12.
반응형

# enum

상수와 같이 다음처럼 원하는 문자등을 선언한다.

    enum Weapon
    {
        Sword,
        Gun,
        Bat
    }

 

내부적으로는 0, 1, 2, ... 로 저장된다.

즉, 위 Weapon.Sword 는 0, Weapon.Gun 은 1 이 내부적으로 사용된다.

 

그래서 다음과 같이 int 로 캐스팅하면 숫자로 정상 출력되는 것을 확인 할 수 있다.

    enum Weapon
    {
        Sword,
        Gun,
        Bat
    }
    
        void Start()
    {
        Weapon myWeapon = Weapon.Sword;
        
        Debug.Log(myWeapon); // Sword
        
        Debug.Log((int) myWeapon); // 0
    }

 

 

# 구조체 struct

여러 데이터( 필드 ) 들의 모음.

    struct Human
    {
        public string name;
        public float heigth;
        public float weight;
        public float feetsize;
    }
    
    void Start()
    {
        Human charles = new Human();
        charles.name = "철수";
        charles.heigth = 198f;
        charles.weight = 52;
        charles.feetsize = 265;
        
        Debug.Log("name : " + charles.name + "height : " + charles.heigth);
    }

 

자바 클래스 처럼 사용할 수 있는 것 같다.

'Dev > C#' 카테고리의 다른 글

추상 메소드, 추상 클래스, 인터페이스  (1) 2024.12.12
상속, 오버라이딩  (0) 2024.12.12