次の方法で共有


Enum.ToString メソッド ()

このインスタンスの値を、それと等価の文字列形式に変換します。

Overrides Overloads Public Function ToString() As String
[C#]
public override string ToString();
[C++]
public: String* ToString();
[JScript]
public override function ToString() : String;

戻り値

このインスタンスの値の文字列形式。

解説

このメソッドは、一般書式指定文字 "G" が指定された場合と同じように機能します。つまり、 FlagsAttribute がこの列挙型に適用されておらず、対象のインスタンスの値と等しい名前付き定数がある場合、戻り値は、その定数の名前が含まれた文字列になります。 FlagsAttribute が適用され、対象のインスタンスの値に等しい 1 つ以上の名前付き定数の組み合わせがある場合、戻り値は、これらの定数の名前を区切り記号で区切ったリストが含まれた文字列になります。それ以外の場合、戻り値は、このインスタンスの数値の文字列形式になります。

書式指定文字の詳細については、 Format メソッドの解説を参照してください。書式指定一般の詳細については、「 書式設定の概要 」を参照してください。

.NET Compact Framework - Windows CE .NET プラットフォームに関する注意点: このメソッドはメタデータ テーブルを検索するため、システム リソースを大量に消費し、パフォーマンスに影響を及ぼすことがあります。

使用例

[Visual Basic, C#, C++] 列挙体を文字列に変換する方法を次のコード例に示します。

 
Imports System

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'.

[C#] 
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'.
*/

[C++] 
#using <mscorlib.dll>
using namespace System;

public __gc class EnumSample {
public:
    __value enum Colors {Red = 1, Blue = 2};
    
    static void main() {
        Enum* myColors = __box(Colors::Red);
        Console::WriteLine(S"The value of this instance is '{0}'", myColors);
    }
};

int main() {
    EnumSample::main();
}
/*
Output.
The value of this instance is 'Red'.
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Enum クラス | Enum メンバ | System 名前空間 | Enum.ToString オーバーロードの一覧 | Format | Parse