본문 바로가기
C#

열거형enum에서 상수값의 배열을 검색하는 getvalue

by g-builder 2024. 9. 25.

열거형enum을 foreach에서 배열의 length처럼 길이로 조건문을 지정하려니 length가 안됬다.

그래서 팀원분께 헬프를 요청하고 같이 코드를 보는데 팀원분께서 처음엔 count로 안되는지 보라해서 했는데 안된다.

팀원분도 같이 고민해주시다가 검색으로 enum.getvalue를 사용해보라고 하셧다.

됬다. 당장 급한불은 껏으니 잠시 다른걸 작성하다가 끝내고 검색해봤다.

다음은 ms에서 제공해주는 예제다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
 
public class GetValuesTest {
    enum Colors { Red, Green, Blue, Yellow };
    enum Styles { Plaid = 0, Striped = 23, Tartan = 65, Corduroy = 78 };
 
    public static void Main() {
 
        Console.WriteLine("The values of the Colors Enum are:");
        foreach(int i in Enum.GetValues(typeof(Colors)))
            Console.WriteLine(i);
 
        Console.WriteLine();
 
        Console.WriteLine("The values of the Styles Enum are:");
        foreach(int i in Enum.GetValues(typeof(Styles)))
            Console.WriteLine(i);
    }
}
// The example produces the following output:
//       The values of the Colors Enum are:
//       0
//       1
//       2
//       3
//
//       The values of the Styles Enum are:
//       0
//       23
//       65
//       78
cs

출저.

https://learn.microsoft.com/ko-kr/dotnet/api/system.enum.getvalues?view=net-8.0

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

Dictionary(딕셔너리) 설명과 사용법  (0) 2024.10.25