Enum.ToString Méthode

Définition

Convertit la valeur de cette instance en sa représentation sous forme de chaîne équivalente.

Surcharges

ToString(String, IFormatProvider)
Obsolète.
Obsolète.

Cette surcharge de méthode est obsolète ; utilisez ToString(String).

ToString()

Convertit la valeur de cette instance en sa représentation sous forme de chaîne équivalente.

ToString(String)

Convertit la valeur de cette instance en sa représentation sous forme de chaîne équivalente à l'aide du format spécifié.

ToString(IFormatProvider)
Obsolète.
Obsolète.

Cette surcharge de méthode est obsolète ; utilisez ToString().

ToString(String, IFormatProvider)

Attention

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

Attention

The provider argument is not used. Use ToString(String) instead.

Cette surcharge de méthode est obsolète ; utilisez 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

Paramètres

format
String

Spécification de format.

provider
IFormatProvider

(Obsolète.)

Retours

Représentation sous forme de chaîne de la valeur de cette instance, comme indiqué par format.

Implémente

Attributs

Exceptions

format ne contient pas une spécification de format valide.

format égale "X", mais le type d'énumération est inconnu.

Remarques

Le format paramètre peut être l’une des chaînes de format suivantes : « G » ou « g », « D » ou « d », « X » ou « x », et « F » ou « f » (la chaîne de format ne respecte pas la casse). Si format est null ou une chaîne vide («  »), le spécificateur de format général (« G ») est utilisé. Pour plus d’informations sur les chaînes de format d’énumération et la mise en forme des valeurs d’énumération, consultez Chaînes de format d’énumération. Pour plus d’informations sur la mise en forme en général, consultez Mise en forme des types.

Spécifiez uniquement format; le provider paramètre est obsolète.

Voir aussi

S’applique à

ToString()

Convertit la valeur de cette instance en sa représentation sous forme de chaîne équivalente.

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

Retours

Représentation sous forme de chaîne de la valeur de cette instance.

Exemples

L’exemple suivant illustre la conversion d’une valeur énumérée en chaîne.

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

Remarques

La valeur de retour est mise en forme avec le spécificateur de format général (« G »). Autrement dit, si le FlagsAttribute n’est pas appliqué à ce type énuméré et qu’il existe une constante nommée égale à la valeur de cette instance, la valeur de retour est une chaîne contenant le nom de la constante. Si le FlagsAttribute est appliqué et qu’il existe une combinaison d’une ou plusieurs constantes nommées égales à la valeur de cette instance, la valeur de retour est une chaîne contenant une liste séparée par un délimiteur des noms des constantes. Sinon, la valeur de retour est la représentation sous forme de chaîne de la valeur numérique de cette instance. Pour plus d’informations sur la mise en forme des valeurs d’énumération, consultez Chaînes de format d’énumération. Pour plus d’informations sur la mise en forme en général, consultez Mise en forme des types.

Notes pour les appelants

Si plusieurs membres d’énumération ont la même valeur sous-jacente et que vous tentez de récupérer la représentation sous-jacente du nom d’un membre d’énumération en fonction de sa valeur sous-jacente, votre code ne doit pas faire d’hypothèses sur le nom que la méthode retournera. Par exemple, l’énumération suivante définit deux membres, Shade.Gray et Shade.Grey, qui ont la même valeur sous-jacente.

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

L’appel de méthode suivant tente de récupérer le nom d’un membre de l’énumération dont la Shade valeur sous-jacente est 1. La méthode peut retourner « Gray » ou « Grey », et votre code ne doit pas faire d’hypothèses sur la chaîne qui sera retournée.

string shadeName = ((Shade) 1).ToString();
let shadeName = (enum<Shade> 1).ToString()
Dim shadeName As String = CType(1, Shade).ToString()

Voir aussi

S’applique à

ToString(String)

Convertit la valeur de cette instance en sa représentation sous forme de chaîne équivalente à l'aide du format spécifié.

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

Paramètres

format
String

Chaîne de format.

Retours

Représentation sous forme de chaîne de la valeur de cette instance, comme indiqué par format.

Exceptions

format contient une spécification non valide.

format égale "X", mais le type d'énumération est inconnu.

Exemples

L’exemple suivant montre comment convertir une valeur énumérée en chaîne.

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

Remarques

Le format paramètre peut être l’une des chaînes de format suivantes : « G » ou « g », « D » ou « d », « X » ou « x », et « F » ou « f » (la chaîne de format ne respecte pas la casse). Si format est null ou une chaîne vide («  »), le spécificateur de format général (« G ») est utilisé. Pour plus d’informations sur les chaînes de format d’énumération et la mise en forme des valeurs d’énumération, consultez Chaînes de format d’énumération. Pour plus d’informations sur la mise en forme en général, consultez Mise en forme des types.

Notes pour les appelants

Si plusieurs membres d’énumération ont la même valeur sous-jacente et que vous tentez de récupérer la représentation sous-jacente du nom d’un membre d’énumération en fonction de sa valeur sous-jacente, votre code ne doit pas faire d’hypothèses sur le nom que la méthode retournera. Par exemple, l’énumération suivante définit deux membres, Shade.Gray et Shade.Grey, qui ont la même valeur sous-jacente.

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

L’appel de méthode suivant tente de récupérer le nom d’un membre de l’énumération dont la Shade valeur sous-jacente est 1. La méthode peut retourner « Gray » ou « Grey », et votre code ne doit pas faire d’hypothèses sur la chaîne qui sera retournée.

string shadeName = ((Shade) 1).ToString("F");
let shadeName = (enum<Shade> 1).ToString "F"
Dim shadeName As String = CType(1, Shade).ToString("F")

Voir aussi

S’applique à

ToString(IFormatProvider)

Attention

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

Attention

The provider argument is not used. Use ToString() instead.

Cette surcharge de méthode est obsolète ; utilisez 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

Paramètres

provider
IFormatProvider

(obsolète)

Retours

Représentation sous forme de chaîne de la valeur de cette instance.

Implémente

Attributs

S’applique à