열거형 형식 문자열

Enum.ToString 메서드를 사용하여 열거형 멤버의 숫자, 16진수 또는 문자열 값을 나타내는 새 문자열 개체를 만들 수 있습니다. 이 메서드는 열거형 형식 문자열 중 하나를 사용하여 반환할 값을 지정합니다.

다음 섹션에서는 열거형 형식 문자열과 반환되는 값을 보여 줍니다. 이러한 형식 지정자는 대/소문자를 구분하지 않습니다.

G 또는 g

가능한 경우 열거형 항목을 문자열 값으로 표시하고, 가능하지 않은 경우 현재 인스턴스의 정수 값을 표시합니다. FlagsAttribute을 설정하여 열거형을 정의한 경우 유효한 각 항목의 문자열 값이 쉼표로 구분되어 서로 연결됩니다. Flags 특성을 설정하지 않은 경우 잘못된 값이 숫자 항목으로 표시됩니다. 다음 예제에서는 G 형식 지정자를 보여 줍니다.

Console.WriteLine(((DayOfWeek)7).ToString("G"));      // 7
Console.WriteLine(ConsoleColor.Red.ToString("G"));    // Red

var attributes = FileAttributes.Hidden | FileAttributes.Archive;
Console.WriteLine(attributes.ToString("G"));          // Hidden, Archive
Console.WriteLine((CType(7, DayOfWeek)).ToString("G"))    ' 7
Console.WriteLine(ConsoleColor.Red.ToString("G"))         ' Red
Dim attributes As FileAttributes = FileAttributes.Hidden Or _
                                   FileAttributes.Archive
Console.WriteLine(attributes.ToString("G"))               ' Hidden, Archive

F 또는 f

열거형 항목을 문자열 값으로 표시합니다(가능한 경우). Flags 특성이 없어도 열거형 항목의 총합으로 값을 표시할 수 있는 경우 유효한 각 항목의 문자열 값이 쉼표로 구분되어 서로 연결됩니다. 열거형 항목으로 값을 확인할 수 없는 경우에는 값의 형식이 정수 값으로 지정됩니다. 다음 예제에서는 F 형식 지정자를 보여 줍니다.

Console.WriteLine(((DayOfWeek)7).ToString("F"));       // Monday, Saturday
Console.WriteLine(ConsoleColor.Blue.ToString("F"));    // Blue

var attributes = FileAttributes.Hidden | FileAttributes.Archive;
Console.WriteLine(attributes.ToString("F"));           // Hidden, Archive
Console.WriteLine((CType(7, DayOfWeek)).ToString("F"))    ' Monday, Saturday
Console.WriteLine(ConsoleColor.Blue.ToString("F"))        ' Blue
Dim attributes As FileAttributes = FileAttributes.Hidden Or _
                                   FileAttributes.Archive
Console.WriteLine(attributes.ToString("F"))               ' Hidden, Archive

D 또는 d

열거형 항목을 가능한 가장 짧은 표현의 정수 값으로 표시합니다. 다음 예제에서는 D 형식 지정자를 보여 줍니다.

Console.WriteLine(((DayOfWeek)7).ToString("D"));       // 7
Console.WriteLine(ConsoleColor.Cyan.ToString("D"));    // 11

var attributes = FileAttributes.Hidden | FileAttributes.Archive;
Console.WriteLine(attributes.ToString("D"));           // 34
Console.WriteLine((CType(7, DayOfWeek)).ToString("D"))     ' 7
Console.WriteLine(ConsoleColor.Cyan.ToString("D"))         ' 11
Dim attributes As FileAttributes = FileAttributes.Hidden Or _
                                   FileAttributes.Archive
Console.WriteLine(attributes.ToString("D"))                ' 34

X 또는 x

열거형 항목을 16진수 값으로 표시합니다. 결과 문자열이 열거형의 기본 숫자 형식에서 바이트마다 2개 문자를 갖도록 하기 위해 필요에 따라 앞에 0이 오는 값으로 표현됩니다. 다음 예제에서는 X 형식 지정자를 보여 줍니다. 예제에서 DayOfWeek, ConsoleColorFileAttributes의 기본 형식은 Int32 또는 8자 결과 문자열을 생성하는 32비트(또는 4바이트) 정수입니다.

Console.WriteLine(((DayOfWeek)7).ToString("X"));       // 00000007
Console.WriteLine(ConsoleColor.Cyan.ToString("X"));    // 0000000B

var attributes = FileAttributes.Hidden | FileAttributes.Archive;
Console.WriteLine(attributes.ToString("X"));           // 00000022
Console.WriteLine((CType(7, DayOfWeek)).ToString("X"))    ' 00000007
Console.WriteLine(ConsoleColor.Cyan.ToString("X"))        ' 0000000B
Dim attributes As FileAttributes = FileAttributes.Hidden Or _
                                   FileAttributes.Archive
Console.WriteLine(attributes.ToString("X"))               ' 00000022

예시

다음 예제에서는 세 개의 항목 Red, BlueGreen으로 구성된 Colors라는 열거형을 정의합니다.

public enum Color { Red = 1, Blue = 2, Green = 3 };
Public Enum Color
    Red = 1
    Blue = 2
    Green = 3
End Enum

열거형이 정의되면 다음과 같이 인스턴스를 선언할 수 있습니다.

Color myColor = Color.Green;
Dim myColor As Color = Color.Green

그런 다음 Color.ToString(System.String) 메서드를 사용하여 전달된 형식 지정자에 따라 열거형 값을 다양한 방식으로 표시할 수 있습니다.

Console.WriteLine("The value of myColor is {0}.",
                  myColor.ToString("G"));
Console.WriteLine("The value of myColor is {0}.",
                  myColor.ToString("F"));
Console.WriteLine("The value of myColor is {0}.",
                  myColor.ToString("D"));
Console.WriteLine("The value of myColor is 0x{0}.",
                  myColor.ToString("X"));
// The example displays the following output to the console:
//       The value of myColor is Green.
//       The value of myColor is Green.
//       The value of myColor is 3.
//       The value of myColor is 0x00000003.
Console.WriteLine("The value of myColor is {0}.", _
                  myColor.ToString("G"))
Console.WriteLine("The value of myColor is {0}.", _
                  myColor.ToString("F"))
Console.WriteLine("The value of myColor is {0}.", _
                  myColor.ToString("D"))
Console.WriteLine("The value of myColor is 0x{0}.", _
                  myColor.ToString("X"))
' The example displays the following output to the console:
'       The value of myColor is Green.
'       The value of myColor is Green.
'       The value of myColor is 3.
'       The value of myColor is 0x00000003.      

참고 항목