Enum을 byte로 쓰고 싶었다..

1. Enum 타입의 내부 기본 타입 변경

Enum 변수를 선언 할 때 기본적으로 데이터 형이 Int32 형으로 선언 된다.

메모리 사용을 조금이라도 줄일기 위해서 숫자가 크지 않는 경우에는 byte를 사용 하고 싶을 때가 있다.

Enum 형의 기본 데이터 형을 변경 하려면 다음과 같이 하면 된다.

public enum Days : byte

{

Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday

};

2. Enum 타입의 내부 기본 타입 얻기

Enum.GetUnderlyingType(enumType);

static void DisplayEnumInfo(Enum enumValue)

{

Type enumType = enumValue.GetType();

Type underlyingType = Enum.GetUnderlyingType(enumType);

Console.WriteLine("{0,-10} {1, 18} {2,15}", enumValue, enumType.Name, underlyingType.Name);

}

// The example displays the following output:

// Member Enumeration Underlying Type //

// Red ConsoleColor Int32

// Monday DayOfWeek Int32

// ToEven MidpointRounding Int32

3. Enum 리스트를 배열로 얻어 오기

Enum.GetValues(enumType);

enum Colors { Red, Green, Blue, Yellow };

foreach(int i in Enum.GetValues(typeof(Colors)))

Console.WriteLine(i);

// The example produces the following output:

// The values of the Colors Enum are:

// 0

// 1

// 2

// 3

Last updated