ETC/끄적끄적

[C#] Enum의 모든 값 가져오기

calci 2021. 6. 30. 19:29

https://docs.microsoft.com/ko-kr/dotnet/api/system.enum.getvalues

 

Enum.GetValues 메서드 (System)

지정된 열거형에서 상수 값의 배열을 검색합니다.Retrieves an array of the values of the constants in a specified enumeration.

docs.microsoft.com

 

사용자가 정의한 Enum을 모두 가져와 루프를 돌고 싶을 때가 있다. Enum.GetValules(Type type) 메서드를 이용해 배열로 받아 올 수 있다. foreach를 사용하는 경우 해당 타입으로 

 

public enum EGameState {
  None,
  Wait,
  PreBattle,
  InBattle,
  PostBattle,
  Result,
  Warp,
}

foreach (EGameState state in Enum.GetValues(typeof(EGameState))) {
  Debug.Log(state.ToString());
}