C#/개인공부

열거형 복습

Bueong_E 2023. 1. 14. 17:54
반응형
SMALL

App Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static reviewAll01.Earth;
//using static reviewAll01.Earth; // 2022기능으로 알아서 클래스를 가져오더라

namespace reviewAll01
{
    class App
    {

        Earth earth = new Earth();  // 클래스 생성
        public App()
        {
            //enum * 열거형식 복습
            
            Console.WriteLine(Seasons.Summer); //Seasons 타입의변수명 출력
            Console.WriteLine((int)Seasons.Summer); // 미리 할당된 변수값 0 출력
            Console.WriteLine(Seasons.Summer.ToString()); // string으로 변수명 출력
            Console.WriteLine((int)Seasons.Summer == 1); //  int타입으로 변경 True            
            string result = "Summer";
            Console.WriteLine(result == Seasons.Summer.ToString()); // string type 변수명과 비교 True
            Console.WriteLine("Summer" == Seasons.Summer.ToString()); // string type 값과 비교 True

            string winter = "Winter";     // winter 변수 초기화
            Seasons myFavorit;            // 열거형식 변수명 정의
            myFavorit = Seasons.Winter;   // 변수명에 열거형식 상수 할당
            object a = myFavorit.GetType();   //타입 오브젝트 형식의 변수에 할당하기
            object b = winter.GetType();      //타입 오브젝트 형식의 변수에 할당하기
            Console.WriteLine(a); //Seasons 타입
            Console.WriteLine(b); //string 타입
            Console.WriteLine(a == b); //  두 변수 모두 출력 결과는 Wㅑnter이지만 타입은 다르다는 걸 알수 있다.
            Console.WriteLine((float)myFavorit); //열거형식 실수 변환



        }
    }
}

 

Earth Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace reviewAll01
{
    internal class Earth
    {
        public enum Seasons  //enum 클래스 정의 및 사용 
                             //값 할당 하지 않으면 자동으로 값 할당
                             //public 해주지 않으면 program 단에서 사용 불가능 (static도 사용 불가)
        {
            None = -1,
            Spring,
            Summer,
            Winter,
            Fall,

        }
        public Earth() 
        { 

        } 
    }
}

값자기 정리하고 싶어져 정리한 열거형식

사용처를 좀더 확실히 정리 할 필요가 있어보인다.

반응형
LIST