Enum.ToString 메서드를 사용하여 숫자, 16진수 또는 열거형 멤버의 문자열 값을 나타내는 새 문자열 개체를 만들 수 있습니다. 이 메서드는 열거형 형식 문자열 중 하나를 사용하여 반환할 값을 지정합니다.
다음 표에서는 열거형 형식 문자열과 이들이 반환하는 값을 나열합니다. 이 서식 지정자는 대/소문자를 구분하지 않습니다.
예제
다음 예제에서는 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.