Enum.ToString 方法

定义

将此实例的值转换为其等效的字符串表示形式。Converts the value of this instance to its equivalent string representation.

重载

ToString(String, IFormatProvider)

此方法重载已过时;请使用 ToString(String)This method overload is obsolete; use ToString(String).

ToString(String)

使用指定格式将此实例的值转换成其等效的字符串表示。Converts the value of this instance to its equivalent string representation using the specified format.

ToString(IFormatProvider)

此方法重载已过时;请使用 ToString()This method overload is obsolete; use ToString().

ToString()

将此实例的值转换为其等效的字符串表示形式。Converts the value of this instance to its equivalent string representation.

ToString(String, IFormatProvider)

警告

此 API 现已过时。

此方法重载已过时;请使用 ToString(String)This method overload is obsolete; use 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);
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, provider As IFormatProvider) As String

参数

format
String

格式规范。A format specification.

provider
IFormatProvider

(已过时。)(Obsolete.)

返回

此实例的值的字符串表示形式,由 format 指定。The string representation of the value of this instance as specified by format.

实现

属性

异常

format 不包含有效的格式规范。format does not contain a valid format specification.

format 等于“X”,但枚举类型未知。format equals "X", but the enumeration type is unknown.

注解

format 参数可以是以下格式字符串之一: "G" 或 "g"、"D"、"d"、"X" 或 "x"、"F" 或 "f" (格式字符串不区分大小写)。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). 如果 format null 或空字符串(""),则使用常规格式说明符("G")。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.

仅指定 format;provider 参数已过时。Specify only format; the provider parameter is obsolete.

另请参阅

ToString(String)

使用指定格式将此实例的值转换成其等效的字符串表示。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);
override this.ToString : string -> string
Public Function ToString (format As String) As String

参数

format
String

一个格式字符串。A format string.

返回

此实例的值的字符串表示形式,由 format 指定。The string representation of the value of this instance as specified by format.

异常

format 包含无效的规范。format contains an invalid specification.

format 等于“X”,但枚举类型未知。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 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)
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"、"F" 或 "f" (格式字符串不区分大小写)。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). 如果 format null 或空字符串(""),则使用常规格式说明符("G")。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.

[!code-csharpSystem.Enum.ToString#1] [!code-vbSystem.Enum.ToString#1][!code-csharpSystem.Enum.ToString#1] [!code-vbSystem.Enum.ToString#1] 下面的方法调用尝试检索其基础值为1的阴影枚举成员的名称。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.

[!code-csharpSystem.Enum.ToString#3] [!code-vbSystem.Enum.ToString#3][!code-csharpSystem.Enum.ToString#3] [!code-vbSystem.Enum.ToString#3]

另请参阅

ToString(IFormatProvider)

警告

此 API 现已过时。

此方法重载已过时;请使用 ToString()This method overload is obsolete; use ToString().

public:
 virtual System::String ^ ToString(IFormatProvider ^ provider);
[System.Obsolete("The provider argument is not used. Please use ToString().")]
public string ToString (IFormatProvider provider);
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String

参数

provider
IFormatProvider

(已过时)(obsolete)

返回

此实例的值的字符串表示形式。The string representation of the value of this instance.

实现

属性

ToString()

将此实例的值转换为其等效的字符串表示形式。Converts the value of this instance to its equivalent string representation.

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

返回

此实例的值的字符串表示形式。The string representation of the value of this instance.

示例

下面的示例演示如何将枚举值转换为字符串。The following example demonstrates converting an enumerated value to a 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'.
*/
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")进行格式设置。The return value is formatted with the general format specifier ("G"). 也就是说,如果 FlagsAttribute 未应用于此枚举类型,并且有一个名为的常量等于此实例的值,则返回值是一个包含该常量的名称的字符串。That is, if the FlagsAttribute is not applied to this enumerated type and there is a named constant equal to the value of this instance, then the return value is a string containing the name of the constant. 如果应用了 FlagsAttribute,并且一个或多个命名常量的组合与此实例的值相等,则返回值是一个字符串,其中包含以分隔符分隔的常量名称列表。If the FlagsAttribute is applied and there is a combination of one or more named constants equal to the value of this instance, then the return value is a string containing a delimiter-separated list of the names of the constants. 否则,返回值为此实例的数字值的字符串表示形式。Otherwise, the return value is the string representation of the numeric value of this instance. 有关设置枚举值的格式的详细信息,请参阅枚举格式字符串For more information about 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.

[!code-csharpSystem.Enum.ToString#1] [!code-vbSystem.Enum.ToString#1][!code-csharpSystem.Enum.ToString#1] [!code-vbSystem.Enum.ToString#1] 下面的方法调用尝试检索其基础值为1的阴影枚举成员的名称。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.

[!code-csharpSystem.Enum.ToString#2] [!code-vbSystem.Enum.ToString#2][!code-csharpSystem.Enum.ToString#2] [!code-vbSystem.Enum.ToString#2]

另请参阅

适用于