열거형 형식 문자열

업데이트: 2007년 11월

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

다음 표에서는 열거형 형식 문자열과 이들이 반환하는 값을 나열합니다. 이 형식 지정자는 대/소문자를 구분하지 않습니다.

형식 문자열

결과

G 또는 g

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

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

F 또는 f

가능하면 열거 엔트리를 문자열 값으로 표시합니다. Flags 특성이 제시되지 않더라도 값이 열거 엔트리의 합으로 완전히 표시될 수 있으면, 유효한 각 엔트리의 문자열 값은 서로 연결되며 쉼표로 구분됩니다. 열거 엔트리에서 이 값을 완전히 결정할 수 없으면 이 값의 형식은 정수 값으로 지정됩니다. 다음 예제에서는 F 형식 지정자를 보여 줍니다.

Console.WriteLine(ConsoleColor.Blue.ToString("F")) ' Displays Blue
Dim attributes As FileAttributes = FileAttributes.Hidden Or _
FileAttributes.Archive
Console.WriteLine(attributes.ToString("F")) ' Displays Hidden, Archive
Console.WriteLine(ConsoleColor.Blue.ToString("F")); // Displays Blue
FileAttributes attributes = FileAttributes.Hidden |
FileAttributes.Archive;
Console.WriteLine(attributes.ToString("F")); // Displays Hidden, Archive

D 또는 d

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

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

X 또는 x

열거 엔트리를 16진수 값으로 표시합니다. 이 값은 최소 8자리로 만들기 위해 앞에 필요한 만큼 0을 붙여서 표시합니다. 다음 예제에서는 X 형식 지정자를 보여 줍니다.

Console.WriteLine(ConsoleColor.Cyan.ToString("X")) ' Displays 0000000B
Dim attributes As FileAttributes = FileAttributes.Hidden Or _
FileAttributes.Archive
Console.WriteLine(attributes.ToString("X")) ' Displays 00000022
Console.WriteLine(ConsoleColor.Cyan.ToString("X")); // Displays 0000000B
FileAttributes attributes = FileAttributes.Hidden |
FileAttributes.Archive;
Console.WriteLine(attributes.ToString("X")); // Displays 00000022

예제

다음 예제에서는 Red, Blue 및 Green의 세 엔트리로 구성되는 Colors라는 열거형을 정의합니다.

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

열거를 정의한 후에는 다음과 같은 방식으로 인스턴스를 선언할 수 있습니다.

Dim myColor As Color = Color.Green      
Color myColor = 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.      

참고 항목

기타 리소스

형식 지정