Enum.Format(Type, Object, String) Metode

Definisi

Mengonversi nilai yang ditentukan dari jenis enumerasi tertentu ke representasi string yang setara sesuai dengan format yang ditentukan.

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

Parameter

enumType
Type

Jenis enumerasi nilai yang akan dikonversi.

value
Object

Nilai yang akan dikonversi.

format
String

Format output yang akan digunakan.

Mengembalikan

Representasi string dari value.

Atribut

Pengecualian

Parameter enumType, value, atau format adalah null.

Parameter enumType bukan jenis Enum .

-atau-

value berasal dari enumerasi yang berbeda dalam jenis dari enumType.

-atau-

Jenis bukan jenis value yang mendasar dari enumType.

Parameter format berisi nilai yang tidak valid.

format sama dengan "X", tetapi jenis enumerasi tidak diketahui.

-atau-

.NET 8 dan versi yang lebih baru: enumType adalah jenis enumerasi yang didukung Boolean.

Contoh

Contoh berikut mengilustrasikan penggunaan Format dalam konteks 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.

Keterangan

Tabel berikut ini memperlihatkan nilai yang valid untuk format parameter .

Format Deskripsi
"G" or "g" Jika value sama dengan konstanta enumerasi bernama, nama konstanta tersebut dikembalikan; jika tidak, desimal yang setara value akan dikembalikan.

Misalnya, misalkan satu-satunya konstanta yang dijumlahkan diberi nama Merah, dan nilainya adalah 1. Jika value ditentukan sebagai 1, format ini mengembalikan "Merah". Namun, jika value ditentukan sebagai 2, format ini mengembalikan "2".

-atau-

FlagsAttribute Jika atribut kustom diterapkan ke enumerasi, value diperlakukan sebagai bidang bit yang berisi satu atau beberapa bendera yang terdiri dari satu atau beberapa bit.

Jika value sama dengan kombinasi konstanta enumerasi bernama, daftar yang dipisahkan pemisah dari nama konstanta tersebut dikembalikan. value dicari untuk bendera, dari bendera dengan nilai terbesar ke nilai terkecil. Untuk setiap bendera yang sesuai dengan bidang bit di value, nama konstanta digabungkan ke daftar yang dipisahkan pemisah. Nilai bendera tersebut kemudian dikecualikan dari pertimbangan lebih lanjut, dan pencarian berlanjut untuk bendera berikutnya.

Jika value tidak sama dengan kombinasi konstanta enumerasi bernama, desimal yang setara value dengan dikembalikan.
"X" or "x" value Mewakili dalam format heksadesimal tanpa "0x" terkemuka.
"D" atau "d" value Mewakili dalam bentuk desimal.
"F" atau "f" Bersifat identik dengan "G" atau "g", kecuali bahwa FlagsAttribute tidak diharuskan untuk hadir pada Enum deklarasi.

Berlaku untuk

Lihat juga