Enum.ToString 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将此实例的值转换为其等效的字符串表示形式。
重载
ToString(String, IFormatProvider) |
已过时.
已过时.
此方法重载已过时;请使用 ToString(String)。 |
ToString() |
将此实例的值转换为其等效的字符串表示形式。 |
ToString(String) |
使用指定格式将此实例的值转换成其等效的字符串表示。 |
ToString(IFormatProvider) |
已过时.
已过时.
此方法重载已过时;请使用 ToString()。 |
ToString(String, IFormatProvider)
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- Enum.cs
注意
The provider argument is not used. Please use ToString(String).
注意
The provider argument is not used. Use ToString(String) instead.
此方法重载已过时;请使用 ToString(String)。
public:
virtual System::String ^ ToString(System::String ^ format, IFormatProvider ^ provider);
[System.Obsolete("The provider argument is not used. Please use ToString(String).")]
public string ToString (string? format, IFormatProvider? provider);
[System.Obsolete("The provider argument is not used. Use ToString(String) instead.")]
public string ToString (string? format, IFormatProvider? provider);
[System.Obsolete("The provider argument is not used. Please use ToString(String).")]
public string ToString (string format, IFormatProvider provider);
public string ToString (string format, IFormatProvider provider);
[<System.Obsolete("The provider argument is not used. Please use ToString(String).")>]
override this.ToString : string * IFormatProvider -> string
[<System.Obsolete("The provider argument is not used. Use ToString(String) instead.")>]
override this.ToString : string * IFormatProvider -> string
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, provider As IFormatProvider) As String
参数
- format
- String
格式规范。
- provider
- IFormatProvider
(已过时。)
返回
此实例的值的字符串表示形式,由 format
指定。
实现
- 属性
例外
format
不包含有效的格式规范。
format
等于“X”,但枚举类型未知。
注解
参数 format
可以是以下格式字符串之一:“G”或“g”、“D”或“d”、“X”或“x”, (格式字符串不区分大小写) 。 如果 format
为 null
或空字符串 (“”) ,则使用常规格式说明符 (“G”) 。 有关枚举格式字符串和设置枚举值格式的详细信息,请参阅 枚举格式字符串。 有关一般格式的详细信息,请参阅 格式设置类型。
仅 format
指定 ; provider
参数已过时。
另请参阅
适用于
ToString()
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- Enum.cs
将此实例的值转换为其等效的字符串表示形式。
public:
override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String
返回
此实例的值的字符串表示形式。
示例
以下示例演示如何将枚举值转换为字符串。
using namespace System;
public ref class EnumSample
{
public:
enum class Colors
{
Red = 1,
Blue = 2
};
static void main()
{
Enum ^ myColors = Colors::Red;
Console::WriteLine( "The value of this instance is '{0}'", myColors );
}
};
int main()
{
EnumSample::main();
}
/*
Output.
The value of this instance is 'Red'.
*/
using System;
public class EnumSample {
enum Colors {Red = 1, Blue = 2};
public static void Main() {
Enum myColors = Colors.Red;
Console.WriteLine("The value of this instance is '{0}'",
myColors.ToString());
}
}
/*
Output.
The value of this instance is 'Red'.
*/
type Colors =
| Red = 1
| Blue = 2
let myColors = Colors.Red
printfn $"The value of this instance is '{myColors.ToString()}'"
// Output.
// The value of this instance is 'Red'.
Public Class EnumSample
Enum Colors
Red = 1
Blue = 2
End Enum
Public Shared Sub Main()
Dim myColors As Colors = Colors.Red
Console.WriteLine("The value of this instance is '{0}'", _
myColors.ToString())
End Sub
End Class
'Output.
'The value of this instance is 'Red'.
注解
返回值使用常规格式说明符 (“G”) 格式化。 也就是说,如果未 FlagsAttribute 应用于此枚举类型,并且有一个等于此实例的值的命名常量,则返回值是包含该常量名称的字符串。 如果应用 了 , FlagsAttribute 并且有一个或多个命名常量的组合等于此实例的值,则返回值是包含常量名称的分隔符分隔列表的字符串。 否则,返回值是此实例的数值的字符串表示形式。 有关设置枚举值格式的详细信息,请参阅 枚举格式字符串。 有关一般格式的详细信息,请参阅 格式设置类型。
调用方说明
如果多个枚举成员具有相同的基础值,并且你尝试根据枚举成员的基础值检索枚举成员名称的字符串表示形式,则代码不应对该方法将返回的名称做出任何假设。 例如,以下枚举定义了两个具有相同基础值的成员 Shade.Gray
和 Shade.Grey
。
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
以下方法调用尝试检索其基础值为 1 的 Shade
枚举成员的名称。 方法可以返回“Gray”或“Gray”,并且代码不应对将返回哪个字符串做出任何假设。
string shadeName = ((Shade) 1).ToString();
let shadeName = (enum<Shade> 1).ToString()
Dim shadeName As String = CType(1, Shade).ToString()
另请参阅
适用于
ToString(String)
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- Enum.cs
使用指定格式将此实例的值转换成其等效的字符串表示。
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
参数
- format
- String
一个格式字符串。
返回
此实例的值的字符串表示形式,由 format
指定。
例外
format
包含无效的规范。
format
等于“X”,但枚举类型未知。
示例
以下示例演示如何将枚举值转换为字符串。
// Sample for Enum::ToString(String)
using namespace System;
public enum class Colors
{
Red, Green, Blue, Yellow = 12
};
int 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)
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
'
注解
参数 format
可以是以下格式字符串之一:“G”或“g”、“D”或“d”、“X”或“x”, (格式字符串不区分大小写) 。 如果 format
为 null
或空字符串 (“”) ,则使用常规格式说明符 (“G”) 。 有关枚举格式字符串和设置枚举值格式的详细信息,请参阅 枚举格式字符串。 有关一般格式的详细信息,请参阅 格式设置类型。
调用方说明
如果多个枚举成员具有相同的基础值,并且你尝试根据枚举成员的基础值检索枚举成员名称的字符串表示形式,则代码不应对该方法将返回的名称做出任何假设。 例如,以下枚举定义了两个具有相同基础值的成员 Shade.Gray
和 Shade.Grey
。
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
以下方法调用尝试检索其基础值为 1 的 Shade
枚举成员的名称。 方法可以返回“Gray”或“Gray”,并且代码不应对将返回哪个字符串做出任何假设。
string shadeName = ((Shade) 1).ToString("F");
let shadeName = (enum<Shade> 1).ToString "F"
Dim shadeName As String = CType(1, Shade).ToString("F")
另请参阅
- Format(Type, Object, String)
- Parse(Type, String)
- ToString(String, IFormatProvider)
- 设置 .NET 中类型的格式
- Enumeration Format Strings
适用于
ToString(IFormatProvider)
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- Enum.cs
注意
The provider argument is not used. Please use ToString().
注意
The provider argument is not used. Use ToString() instead.
此方法重载已过时;请使用 ToString()。
public:
virtual System::String ^ ToString(IFormatProvider ^ provider);
[System.Obsolete("The provider argument is not used. Please use ToString().")]
public string ToString (IFormatProvider? provider);
[System.Obsolete("The provider argument is not used. Use ToString() instead.")]
public string ToString (IFormatProvider? provider);
[System.Obsolete("The provider argument is not used. Please use ToString().")]
public string ToString (IFormatProvider provider);
public string ToString (IFormatProvider provider);
[<System.Obsolete("The provider argument is not used. Please use ToString().")>]
override this.ToString : IFormatProvider -> string
[<System.Obsolete("The provider argument is not used. Use ToString() instead.")>]
override this.ToString : IFormatProvider -> string
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String
参数
- provider
- IFormatProvider
(已过时)
返回
此实例的值的字符串表示形式。
实现
- 属性