संपादित करें

इसके माध्यम से साझा किया गया


Enum.Format(Type, Object, String) Method

Definition

Converts the specified value of a specified enumerated type to its equivalent string representation according to the specified format.

public:
 static System::String ^ Format(Type ^ enumType, System::Object ^ value, System::String ^ format);
public static string Format (Type enumType, object value, string format);
[System.Runtime.InteropServices.ComVisible(true)]
public static string Format (Type enumType, object value, string format);
static member Format : Type * obj * string -> string
[<System.Runtime.InteropServices.ComVisible(true)>]
static member Format : Type * obj * string -> string
Public Shared Function Format (enumType As Type, value As Object, format As String) As String

Parameters

enumType
Type

The enumeration type of the value to convert.

value
Object

The value to convert.

format
String

The output format to use.

Returns

A string representation of value.

Attributes

Exceptions

The enumType, value, or format parameter is null.

The enumType parameter is not an Enum type.

-or-

The value is from an enumeration that differs in type from enumType.

-or-

The type of value is not an underlying type of enumType.

The format parameter contains an invalid value.

format equals "X", but the enumeration type is unknown.

-or-

.NET 8 and later versions: enumType is a Boolean-backed enumeration type.

Examples

The following example illustrates the use of Format in the context of Enum.

using namespace System;
public enum class Colors
{
   Red, Green, Blue, Yellow
};

int main()
{
   Colors myColor = Colors::Blue;
   Console::WriteLine(  "My favorite color is {0}.", myColor );
   Console::WriteLine(  "The value of my favorite color is {0}.", Enum::Format( Colors::typeid, myColor,  "d" ) );
   Console::WriteLine(  "The hex value of my favorite color is {0}.", Enum::Format( Colors::typeid, myColor,  "x" ) );
}
// The example displays the folowing output:
//    My favorite color is Blue.
//    The value of my favorite color is 2.
//    The hex value of my favorite color is 00000002.
using System;

enum Colors { Red, Green, Blue, Yellow };

public class FormatTest {
    public static void Main() {
        Colors myColor = Colors.Blue;

        Console.WriteLine("My favorite color is {0}.", myColor);
        Console.WriteLine("The value of my favorite color is {0}.", Enum.Format(typeof(Colors), myColor, "d"));
        Console.WriteLine("The hex value of my favorite color is {0}.", Enum.Format(typeof(Colors), myColor, "x"));
    }
}
// The example displays the following output:
//    My favorite color is Blue.
//    The value of my favorite color is 2.
//    The hex value of my favorite color is 00000002.
open System

type Colors =
    | Red = 0
    | Green = 1
    | Blue = 2
    | Yellow = 3

let myColor = Colors.Blue

printfn $"My favorite color is {myColor}."
printfn $"""The value of my favorite color is {Enum.Format(typeof<Colors>, myColor, "d")}."""
printfn $"""The hex value of my favorite color is {Enum.Format(typeof<Colors>, myColor, "x")}."""
// The example displays the following output:
//    My favorite color is Blue.
//    The value of my favorite color is 2.
//    The hex value of my favorite color is 00000002.
 Enum Colors
     Red
     Green
     Blue
     Yellow    
 End Enum
    
Public Class FormatTest
    Public Shared Sub Main()
        Dim myColor As Colors = Colors.Blue
        
        Console.WriteLine("My favorite color is {0}.", myColor)
        Console.WriteLine("The value of my favorite color is {0}.", [Enum].Format(GetType(Colors), myColor, "d"))
        Console.WriteLine("The hex value of my favorite color is {0}.", [Enum].Format(GetType(Colors), myColor, "x"))
    End Sub 
End Class 
' The example displays the following output:
'    My favorite color is Blue.
'    The value of my favorite color is 2.
'    The hex value of my favorite color is 00000002.

Remarks

The following table shows the valid values for the format parameter.

Format Description
"G" or "g" If value is equal to a named enumerated constant, the name of that constant is returned; otherwise, the decimal equivalent of value is returned.

For example, suppose the only enumerated constant is named Red, and its value is 1. If value is specified as 1, this format returns "Red". However, if value is specified as 2, this format returns "2".

-or-

If the FlagsAttribute custom attribute is applied to the enumeration, value is treated as a bit field that contains one or more flags that consist of one or more bits.

If value is equal to a combination of named enumerated constants, a delimiter-separated list of the names of those constants is returned. value is searched for flags, going from the flag with the largest value to the smallest value. For each flag that corresponds to a bit field in value, the name of the constant is concatenated to the delimiter-separated list. The value of that flag is then excluded from further consideration, and the search continues for the next flag.

If value is not equal to a combination of named enumerated constants, the decimal equivalent of value is returned.
"X" or "x" Represents value in hexadecimal format without a leading "0x".
"D" or "d" Represents value in decimal form.
"F" or "f" Behaves identically to "G" or "g", except that the FlagsAttribute is not required to be present on the Enum declaration.

Applies to

See also