Converts the value of this instance to its equivalent string representation using the specified format.
public:
System::String ^ ToString(System::String ^ format);
public string ToString(string format);
public string ToString(string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String
Parameters
A format string.
ReturnsThe string representation of the value of this instance as specified by format
.
format
contains an invalid specification.
format
equals "X", but the enumeration type is unknown.
The following example demonstrates how to convert an enumerated value to a string.
// Sample for Enum.ToString(String)
using System;
class Sample
{
enum Colors {Red, Green, Blue, Yellow = 12};
public static void Main()
{
Colors myColor = Colors.Yellow;
Console.WriteLine("Colors.Red = {0}", Colors.Red.ToString("d"));
Console.WriteLine("Colors.Green = {0}", Colors.Green.ToString("d"));
Console.WriteLine("Colors.Blue = {0}", Colors.Blue.ToString("d"));
Console.WriteLine("Colors.Yellow = {0}", Colors.Yellow.ToString("d"));
Console.WriteLine("{0}myColor = Colors.Yellow{0}", Environment.NewLine);
Console.WriteLine("myColor.ToString(\"g\") = {0}", myColor.ToString("g"));
Console.WriteLine("myColor.ToString(\"G\") = {0}", myColor.ToString("G"));
Console.WriteLine("myColor.ToString(\"x\") = {0}", myColor.ToString("x"));
Console.WriteLine("myColor.ToString(\"X\") = {0}", myColor.ToString("X"));
Console.WriteLine("myColor.ToString(\"d\") = {0}", myColor.ToString("d"));
Console.WriteLine("myColor.ToString(\"D\") = {0}", myColor.ToString("D"));
Console.WriteLine("myColor.ToString(\"f\") = {0}", myColor.ToString("f"));
Console.WriteLine("myColor.ToString(\"F\") = {0}", myColor.ToString("F"));
}
}
/*
This example produces the following results:
Colors.Red = 0
Colors.Green = 1
Colors.Blue = 2
Colors.Yellow = 12
myColor = Colors.Yellow
myColor.ToString("g") = Yellow
myColor.ToString("G") = Yellow
myColor.ToString("x") = 0000000C
myColor.ToString("X") = 0000000C
myColor.ToString("d") = 12
myColor.ToString("D") = 12
myColor.ToString("f") = Yellow
myColor.ToString("F") = Yellow
*/
// Sample for Enum.ToString(String)
open System
type Colors =
| Red = 0
| Green = 1
| Blue = 2
| Yellow = 12
let myColor = Colors.Yellow
printfn $"""Colors.Red = {Colors.Red.ToString "d"}"""
printfn $"""Colors.Green = {Colors.Green.ToString "d"}"""
printfn $"""Colors.Blue = {Colors.Blue.ToString "d"}"""
printfn $"""Colors.Yellow = {Colors.Yellow.ToString "d"}"""
printfn "\nmyColor = Colors.Yellow\n"
printfn $"""myColor.ToString("g") = {myColor.ToString "g"}"""
printfn $"""myColor.ToString("G") = {myColor.ToString "G"}"""
printfn $"""myColor.ToString("x") = {myColor.ToString "x"}"""
printfn $"""myColor.ToString("X") = {myColor.ToString "X"}"""
printfn $"""myColor.ToString("d") = {myColor.ToString "d"}"""
printfn $"""myColor.ToString("D") = {myColor.ToString "d"}"""
printfn $"""myColor.ToString("f") = {myColor.ToString "f"}"""
printfn $"""myColor.ToString("F") = {myColor.ToString "F"}"""
// This example produces the following results:
// Colors.Red = 0
// Colors.Green = 1
// Colors.Blue = 2
// Colors.Yellow = 12
//
// myColor = Colors.Yellow
//
// myColor.ToString("g") = Yellow
// myColor.ToString("G") = Yellow
// myColor.ToString("x") = 0000000C
// myColor.ToString("X") = 0000000C
// myColor.ToString "d" = 12
// myColor.ToString "d" = 12
// myColor.ToString("f") = Yellow
// myColor.ToString("F") = Yellow
' Sample for Enum.ToString(String)
Class Sample
Enum Colors
Red
Green
Blue
Yellow = 12
End Enum 'Colors
Public Shared Sub Main()
Dim myColor As Colors = Colors.Yellow
Console.WriteLine("Colors.Red = {0}", Colors.Red.ToString("d"))
Console.WriteLine("Colors.Green = {0}", Colors.Green.ToString("d"))
Console.WriteLine("Colors.Blue = {0}", Colors.Blue.ToString("d"))
Console.WriteLine("Colors.Yellow = {0}", Colors.Yellow.ToString("d"))
Console.WriteLine("{0}myColor = Colors.Yellow{0}", Environment.NewLine)
Console.WriteLine("myColor.ToString(""g"") = {0}", myColor.ToString("g"))
Console.WriteLine("myColor.ToString(""G"") = {0}", myColor.ToString("G"))
Console.WriteLine("myColor.ToString(""x"") = {0}", myColor.ToString("x"))
Console.WriteLine("myColor.ToString(""X"") = {0}", myColor.ToString("X"))
Console.WriteLine("myColor.ToString(""d"") = {0}", myColor.ToString("d"))
Console.WriteLine("myColor.ToString(""D"") = {0}", myColor.ToString("D"))
Console.WriteLine("myColor.ToString(""f"") = {0}", myColor.ToString("f"))
Console.WriteLine("myColor.ToString(""F"") = {0}", myColor.ToString("F"))
End Sub
End Class
'
'This example produces the following results:
'
'Colors.Red = 0
'Colors.Green = 1
'Colors.Blue = 2
'Colors.Yellow = 12
'
'myColor = Colors.Yellow
'
'myColor.ToString("g") = Yellow
'myColor.ToString("G") = Yellow
'myColor.ToString("x") = 0000000C
'myColor.ToString("X") = 0000000C
'myColor.ToString("d") = 12
'myColor.ToString("D") = 12
'myColor.ToString("f") = Yellow
'myColor.ToString("F") = Yellow
'
Remarks
The format
parameter can be one of the following format strings: "G" or "g", "D" or "d", "X" or "x", and "F" or "f" (the format string is not case-sensitive). If format
is null
or an empty string (""), the general format specifier ("G") is used. For more information about the enumeration format strings and formatting enumeration values, see Enumeration Format Strings. For more information about formatting in general, see Formatting Types.
If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return. For example, the following enumeration defines two members, Shade.Gray
and Shade.Grey
, that have the same underlying value.
enum Shade
{
White = 0, Gray = 1, Grey = 1, Black = 2
}
type Shade =
| White = 0
| Gray = 1
| Grey = 1
| Black = 2
Public Enum Shade
White = 0
Gray = 1
Grey = 1
Black = 2
End Enum
The following method call attempts to retrieve the name of a member of the Shade
enumeration whose underlying value is 1. The method can return either "Gray" or "Grey", and your code should not make any assumptions about which string will be returned.
string shadeName = ((Shade) 1).ToString("F");
let shadeName = (enum<Shade> 1).ToString "F"
Dim shadeName As String = CType(1, Shade).ToString("F")
See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4