Enum.ToString 方法

定義

將這個執行個體的值轉換為它的相等字串表示。Converts the value of this instance to its equivalent string representation.

多載

ToString(IFormatProvider)
已過時。

這個方法多載已淘汰,請改用 ToString()This method overload is obsolete; use ToString().

ToString(String)

使用指定格式,將這個執行個體的值轉換為它的相等字串表示。Converts the value of this instance to its equivalent string representation using the specified format.

ToString(String, IFormatProvider)
已過時。

這個方法多載已淘汰,請改用 ToString(String)This method overload is obsolete; use ToString(String).

ToString()

將這個執行個體的值轉換為它的相等字串表示。Converts the value of this instance to its equivalent string representation.

ToString(IFormatProvider)

警告

The provider argument is not used. Please use ToString().

這個方法多載已淘汰,請改用 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);
[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
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String

參數

provider
IFormatProvider

(已過時)(obsolete)

傳回

String

這個執行個體值的字串表示。The string representation of the value of this instance.

實作

屬性

適用於

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);
public string ToString (string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String

參數

format
String

格式字串。A format string.

傳回

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). 如果 formatnull 或 ( "" ) 的空字串,則會使用一般格式規範 ( "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.

[!程式碼-csharp system.string. Enum # 1][! code-vb system.string. Enum # 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.

[!程式碼-csharp system.string. Enum # 3][!程式碼-vb system.string. Enum # 3][!code-csharpSystem.Enum.ToString#3] [!code-vbSystem.Enum.ToString#3]

另請參閱

適用於

ToString(String, IFormatProvider)

警告

The provider argument is not used. Please use ToString(String).

這個方法多載已淘汰,請改用 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);
[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
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.)

傳回

String

這個執行個體值的字串表示,如同 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). 如果 formatnull 或 ( "" ) 的空字串,則會使用一般格式規範 ( "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()

將這個執行個體的值轉換為它的相等字串表示。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

傳回

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.

[!程式碼-csharp system.string. Enum # 1][! code-vb system.string. Enum # 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.

[!程式碼-csharp system.string. Enum # 2][! code-vb system.string. Enum # 2][!code-csharpSystem.Enum.ToString#2] [!code-vbSystem.Enum.ToString#2]

另請參閱

適用於