Enum.Format(Type, Object, String) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定した形式に従って、指定した列挙型の指定した値を等価の文字列形式に変換します。
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
パラメーター
- enumType
- Type
変換する値の列挙型。
- value
- Object
変換する値。
- format
- String
使用する出力形式。
返品
valueの文字列形式。
- 属性
例外
enumType、value、またはformatパラメーターがnull。
enumType パラメーターはEnum型ではありません。
-又は-
valueは、型がenumTypeとは異なる列挙型のものです。
-又は-
valueの型は、enumTypeの基になる型ではありません。
format パラメーターに無効な値が含まれています。
format は "X" と等しくなりますが、列挙型は不明です。
-又は-
.NET 8 以降のバージョン: enumType は、ブール値に基づく列挙型です。
例
次の例は、FormatのコンテキストでのEnumの使用を示しています。
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.
注釈
次の表に、 format パラメーターの有効な値を示します。
| フォーマット | Description |
|---|---|
| "G" または "g" |
valueが名前付き列挙定数と等しい場合は、その定数の名前が返されます。それ以外の場合は、10 進数の等価valueが返されます。たとえば、唯一の列挙定数の名前が Red で、その値が 1 であるとします。 valueが 1 として指定されている場合、この形式は "Red" を返します。 ただし、 value が 2 として指定されている場合、この形式は "2" を返します。-又は- FlagsAttributeカスタム属性が列挙型に適用されている場合、 valueは、1 つ以上のビットで構成される 1 つ以上のフラグを含むビット フィールドとして扱われます。value名前付き列挙定数の組み合わせと等しい場合は、それらの定数の名前の区切り記号で区切られたリストが返されます。
value はフラグを検索し、最大値を持つフラグから最小値に移動します。
valueのビット フィールドに対応するフラグごとに、定数の名前が区切り記号で区切られたリストに連結されます。 その後、そのフラグの値はさらなる考慮事項から除外され、次のフラグの検索が続行されます。value名前付き列挙定数の組み合わせと等しくない場合は、10 進数の等価valueが返されます。 |
| "X" または "x" | 先頭に "0x" を付けずに、 value を 16 進数形式で表します。 |
| "D" または "d" |
valueを 10 進形式で表します。 |
| "F" または "f" | FlagsAttribute宣言にEnumが存在する必要がない点を除き、"G" または "g" と同じように動作します。 |