Enum.TryParse メソッド

定義

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。 戻り値は、変換が成功したかどうかを示します。

オーバーロード

TryParse(Type, ReadOnlySpan<Char>, Object)

1 つ以上の列挙定数の名前または数値の文字表現のスパンを等価の列挙オブジェクトに変換します。

TryParse(Type, String, Object)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。

TryParse(Type, ReadOnlySpan<Char>, Boolean, Object)

1 つ以上の列挙定数の名前または数値の文字表現のスパンを等価の列挙オブジェクトに変換します。 演算で大文字と小文字を区別しないかどうかをパラメーターで指定します。

TryParse(Type, String, Boolean, Object)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。

TryParse<TEnum>(ReadOnlySpan<Char>, TEnum)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。

TryParse<TEnum>(String, TEnum)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。 戻り値は、変換が成功したかどうかを示します。

TryParse<TEnum>(String, Boolean, TEnum)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。 演算で大文字と小文字を区別するかどうかをパラメーターで指定します。 戻り値は、変換が成功したかどうかを示します。

TryParse<TEnum>(ReadOnlySpan<Char>, Boolean, TEnum)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。 演算で大文字と小文字を区別するかどうかをパラメーターで指定します。 戻り値は、変換が成功したかどうかを示します。

TryParse(Type, ReadOnlySpan<Char>, Object)

1 つ以上の列挙定数の名前または数値の文字表現のスパンを等価の列挙オブジェクトに変換します。

public:
 static bool TryParse(Type ^ enumType, ReadOnlySpan<char> value, [Runtime::InteropServices::Out] System::Object ^ % result);
public static bool TryParse (Type enumType, ReadOnlySpan<char> value, out object? result);
static member TryParse : Type * ReadOnlySpan<char> * obj -> bool
Public Shared Function TryParse (enumType As Type, value As ReadOnlySpan(Of Char), ByRef result As Object) As Boolean

パラメーター

enumType
Type

解析に使用する列挙型。

value
ReadOnlySpan<Char>

1 つ以上の列挙定数の名前または数値のスパン表現。

result
Object

このメソッドが を true返す場合は、解析された値を表す列挙定数が含まれます。

戻り値

変換が成功した場合は true。それ以外の場合は false

例外

.NET 8 以降のバージョン: enumType は、ブール値に基づく列挙型です。

適用対象

TryParse(Type, String, Object)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。

public:
 static bool TryParse(Type ^ enumType, System::String ^ value, [Runtime::InteropServices::Out] System::Object ^ % result);
public static bool TryParse (Type enumType, string? value, out object? result);
public static bool TryParse (Type enumType, string value, out object result);
static member TryParse : Type * string * obj -> bool
Public Shared Function TryParse (enumType As Type, value As String, ByRef result As Object) As Boolean

パラメーター

enumType
Type

解析に使用する列挙型。

value
String

1 つ以上の列挙定数の名前または数値の文字列表現。

result
Object

このメソッドが を true返す場合は、解析された値を表す列挙定数が含まれます。

戻り値

変換が成功した場合は true。それ以外の場合は false

例外

.NET 8 以降のバージョン: enumType は、ブール値に基づく列挙型です。

適用対象

TryParse(Type, ReadOnlySpan<Char>, Boolean, Object)

1 つ以上の列挙定数の名前または数値の文字表現のスパンを等価の列挙オブジェクトに変換します。 演算で大文字と小文字を区別しないかどうかをパラメーターで指定します。

public:
 static bool TryParse(Type ^ enumType, ReadOnlySpan<char> value, bool ignoreCase, [Runtime::InteropServices::Out] System::Object ^ % result);
public static bool TryParse (Type enumType, ReadOnlySpan<char> value, bool ignoreCase, out object? result);
static member TryParse : Type * ReadOnlySpan<char> * bool * obj -> bool
Public Shared Function TryParse (enumType As Type, value As ReadOnlySpan(Of Char), ignoreCase As Boolean, ByRef result As Object) As Boolean

パラメーター

enumType
Type

解析に使用する列挙型。

value
ReadOnlySpan<Char>

1 つ以上の列挙定数の名前または数値のスパン表現。

ignoreCase
Boolean

大文字と小文字を区別しないモードで enumType を読み取る場合は、true。大文字と小文字を区別するモードで enumType を読み取る場合は false

result
Object

このメソッドが を true返す場合は、解析された値を表す列挙定数が含まれます。

戻り値

変換が成功した場合は true。それ以外の場合は false

例外

.NET 8 以降のバージョン: enumType は、ブール値に基づく列挙型です。

適用対象

TryParse(Type, String, Boolean, Object)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。

public:
 static bool TryParse(Type ^ enumType, System::String ^ value, bool ignoreCase, [Runtime::InteropServices::Out] System::Object ^ % result);
public static bool TryParse (Type enumType, string? value, bool ignoreCase, out object? result);
public static bool TryParse (Type enumType, string value, bool ignoreCase, out object result);
static member TryParse : Type * string * bool * obj -> bool
Public Shared Function TryParse (enumType As Type, value As String, ignoreCase As Boolean, ByRef result As Object) As Boolean

パラメーター

enumType
Type

解析に使用する列挙型。

value
String

1 つ以上の列挙定数の名前または数値の文字列表現。

ignoreCase
Boolean

大文字と小文字を区別しないモードで value を読み取る場合は、true。大文字と小文字を区別するモードで value を読み取る場合は false

result
Object

このメソッドが を true返す場合は、解析された値を表す列挙定数が含まれます。

戻り値

変換が成功した場合は true。それ以外の場合は false

例外

.NET 8 以降のバージョン: enumType は、ブール値に基づく列挙型です。

適用対象

TryParse<TEnum>(ReadOnlySpan<Char>, TEnum)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。

public:
generic <typename TEnum>
 where TEnum : value class static bool TryParse(ReadOnlySpan<char> value, [Runtime::InteropServices::Out] TEnum % result);
public static bool TryParse<TEnum> (ReadOnlySpan<char> value, out TEnum result) where TEnum : struct;
static member TryParse : ReadOnlySpan<char> * 'Enum -> bool (requires 'Enum : struct)
Public Shared Function TryParse(Of TEnum As Structure) (value As ReadOnlySpan(Of Char), ByRef result As TEnum) As Boolean

型パラメーター

TEnum

result オブジェクトの型。

パラメーター

value
ReadOnlySpan<Char>

1 つ以上の列挙定数の名前または数値のスパン表現。

result
TEnum

このメソッドが を true返す場合は、解析された値を表す列挙定数が含まれます。

戻り値

変換が成功した場合は true。それ以外の場合は false

例外

TEnum は列挙型ではありません。

.NET 8 以降のバージョン: TEnum は、ブール値に基づく列挙型です。

適用対象

TryParse<TEnum>(String, TEnum)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。 戻り値は、変換が成功したかどうかを示します。

public:
generic <typename TEnum>
 where TEnum : value class static bool TryParse(System::String ^ value, [Runtime::InteropServices::Out] TEnum % result);
public static bool TryParse<TEnum> (string value, out TEnum result) where TEnum : struct;
public static bool TryParse<TEnum> (string? value, out TEnum result) where TEnum : struct;
static member TryParse : string * 'Enum -> bool (requires 'Enum : struct)
Public Shared Function TryParse(Of TEnum As Structure) (value As String, ByRef result As TEnum) As Boolean

型パラメーター

TEnum

value の変換先の列挙型。

パラメーター

value
String

変換する列挙定数の名前または基になる値の文字列形式。大文字と小文字が区別されます。

result
TEnum

このメソッドが戻るとき、 には、解析操作が成功した場合に によって値が表されるvalueTEnumのオブジェクトが含まれます。 解析操作が失敗した場合は、 の基になる型 TEnumの既定値が格納されます。 このパラメーターは初期化せずに渡されます。

戻り値

value パラメーターが正常に変換された場合は true。それ以外の場合は false

例外

TEnum は列挙型ではありません。

.NET 8 以降のバージョン: TEnum は、ブール値に基づく列挙型です。

次の例では、Colors列挙体を定義し、TryParse<TEnum>(String, TEnum)メソッドを呼び出して文字列を対応する列挙値に変換し、IsDefinedメソッドを呼び出して、特定の整数値がColors列挙体の基になる値であることを確認します。

using System;

[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };

public class Example
{
   public static void Main()
   {
      string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
      foreach (string colorString in colorStrings)
      {
         Colors colorValue;
         if (Enum.TryParse(colorString, out colorValue))
            if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());
            else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
         else
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString);
      }
   }
}
// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       blue is not a member of the Colors enumeration.
//       Converted 'Blue' to Blue.
//       Yellow is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.
open System

[<Flags>]
type Colors =
    | None = 0
    | Red = 1
    | Green = 2
    | Blue = 4

let colorStrings = 
    [ "0"; "2"; "8"; "blue"; "Blue"; "Yellow"; "Red, Green" ]
for colorString in colorStrings do
    match Enum.TryParse colorString with
    | true, colorValue ->
        if Enum.IsDefined(typeof<Colors>, colorValue) || (string colorValue).Contains "," then
            printfn $"Converted '{colorString}' to {colorValue}."
        else
            printfn $"{colorString} is not an underlying value of the Colors enumeration."
    | _ ->
        printfn $"{colorString} is not a member of the Colors enumeration."
// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       blue is not a member of the Colors enumeration.
//       Converted 'Blue' to Blue.
//       Yellow is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.
<Flags> Enum Colors As Integer
   None = 0
   Red = 1
   Green = 2
   Blue = 4
End Enum

Module Example
   Public Sub Main()
      Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow", "Red, Green"}
      For Each colorString As String In colorStrings
         Dim colorValue As Colors
         If [Enum].TryParse(colorString, colorValue) Then        
            If [Enum].IsDefined(GetType(Colors), colorValue) Or colorValue.ToString().Contains(",") Then 
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString())
            Else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString)            
            End If                    
         Else
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString)
         End If
      Next
   End Sub
End Module
' The example displays the following output:
'    Converted '0' to None.
'    Converted '2' to Green.
'    8 is not an underlying value of the Colors enumeration.
'    blue is not a member of the Colors enumeration.
'    Converted 'Blue' to Blue.
'    Yellow is not a member of the Colors enumeration.
'    Converted 'Red, Green' to Red, Green.

注釈

TryParse<TEnum>(String, TEnum)Parse(Type, String)メソッドと同じですが、例外をスローするのではなく、変換に失敗した場合はfalseを返します。 列挙値の文字列表現を解析するときに例外処理を行う必要がなくなります。

valueパラメーターには、列挙メンバーの基になる値または名前付き定数の文字列形式、またはコンマ (,) で区切られた名前付き定数または基になる値のリストが含まれます。 valueに複数の名前付き定数または値が含まれている場合は、valueの各値、名前、またはコンマの前または後に 1 つ以上の空白文字を含めることができます。 valueがリストの場合、resultは、指定された名前または基になる値を、ビットごとにOR演算で組み合わせた値となります。 valueが列挙値の名前の文字列表現である場合、valueと列挙型の比較では大文字と小文字が区別されます。

valueTEnumの名前付き定数に対応しない名前である場合、メソッドはfalseを返します。 valueTEnum列挙体の基になる値を表さない整数の文字列表現の場合、メソッドは、基になる値が整数型に変換されたvalueである列挙体のメンバーを返します。 この動作が望ましくない場合は、IsDefinedメソッドを呼び出して、整数の特定の文字列形式が実際にTEnumのメンバーであることを確認します。

解析操作が失敗した場合、 result パラメーターには既定値の 0 が含まれます。これは、基になる TEnum 列挙体のメンバーではない可能性があります。 の TEnum名前付き定数に値が割り当てられていない場合、既定値は の最初の TEnumメンバーと等しくなります。 それ以外の場合、既定値は、割り当てられた値が 0 の列挙体のメンバーと等しくなります。

こちらもご覧ください

適用対象

TryParse<TEnum>(String, Boolean, TEnum)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。 演算で大文字と小文字を区別するかどうかをパラメーターで指定します。 戻り値は、変換が成功したかどうかを示します。

public:
generic <typename TEnum>
 where TEnum : value class static bool TryParse(System::String ^ value, bool ignoreCase, [Runtime::InteropServices::Out] TEnum % result);
public static bool TryParse<TEnum> (string value, bool ignoreCase, out TEnum result) where TEnum : struct;
public static bool TryParse<TEnum> (string? value, bool ignoreCase, out TEnum result) where TEnum : struct;
static member TryParse : string * bool * 'Enum -> bool (requires 'Enum : struct)
Public Shared Function TryParse(Of TEnum As Structure) (value As String, ignoreCase As Boolean, ByRef result As TEnum) As Boolean

型パラメーター

TEnum

value の変換先の列挙型。

パラメーター

value
String

変換する列挙定数の名前または基になる値の文字列形式。

ignoreCase
Boolean

大文字と小文字を区別しない場合は true。大文字と小文字を区別する場合は false

result
TEnum

このメソッドが戻るとき、 には、解析操作が成功した場合に によって値が表されるvalueTEnumのオブジェクトが含まれます。 解析操作が失敗した場合は、 の基になる型 TEnumの既定値が格納されます。 このパラメーターは初期化せずに渡されます。

戻り値

value パラメーターが正常に変換された場合は true。それ以外の場合は false

例外

TEnum は列挙型ではありません。

.NET 8 以降のバージョン: TEnum は、ブール値に基づく列挙型です。

次の例では、Colors列挙体を定義し、TryParse<TEnum>(String, Boolean, TEnum)メソッドを呼び出して文字列を対応する列挙値に変換し、IsDefinedメソッドを呼び出して、特定の整数値がColors列挙体の基になる値であることを確認します。 TryParse<TEnum>(String, Boolean, TEnum)メソッドは、名前付き定数の文字列形式を等価の列挙値に変換しようとしたときに、大文字と小文字を区別しない比較を使用します。

using System;

[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };

public class Example
{
   public static void Main()
   {
      string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
      foreach (string colorString in colorStrings)
      {
         Colors colorValue;
         if (Enum.TryParse(colorString, true, out colorValue))
            if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());
            else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
         else
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString);
      }
   }
}
// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       Converted 'blue' to Blue.
//       Converted 'Blue' to Blue.
//       Yellow is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.
open System

[<Flags>]
type Colors =
    | None = 0
    | Red = 1
    | Green = 2
    | Blue = 4

let colorStrings = 
    [ "0"; "2"; "8"; "blue"; "Blue"; "Yellow"; "Red, Green" ]
for colorString in colorStrings do
    match Enum.TryParse(colorString, true) with
    | true, colorValue ->
        if Enum.IsDefined(typeof<Colors>, colorValue) || (string colorValue).Contains "," then
            printfn $"Converted '{colorString}' to {colorValue}."
        else
            printfn $"{colorString} is not an underlying value of the Colors enumeration."
    | _ ->
        printfn $"{colorString} is not a member of the Colors enumeration."
// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       Converted 'blue' to Blue.
//       Converted 'Blue' to Blue.
//       Yellow is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.
<Flags> Enum Colors As Integer
   None = 0
   Red = 1
   Green = 2
   Blue = 4
End Enum

Module Example
   Public Sub Main()
      Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow", "Red, Green"}
      For Each colorString As String In colorStrings
         Dim colorValue As Colors
         If [Enum].TryParse(colorString, True, colorValue) Then        
            If [Enum].IsDefined(GetType(Colors), colorValue) Or colorValue.ToString().Contains(",") Then 
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString())
            Else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString)            
            End If                    
         Else
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString)
         End If
      Next
   End Sub
End Module
' The example displays the following output:
'       Converted '0' to None.
'       Converted '2' to Green.
'       8 is not an underlying value of the Colors enumeration.
'       Converted 'blue' to Blue.
'       Converted 'Blue' to Blue.
'       Yellow is not a member of the Colors enumeration.
'       Converted 'Red, Green' to Red, Green.

注釈

TryParse<TEnum>(String, Boolean, TEnum)Parse(Type, String, Boolean)メソッドと同じですが、例外をスローするのではなく、変換に失敗した場合はfalseを返します。 列挙値の文字列表現を解析するときに例外処理を行う必要がなくなります。

valueパラメーターには、列挙メンバーの基になる値または名前付き定数の文字列形式、またはコンマ (,) で区切られた名前付き定数または基になる値のリストが含まれます。 valueに複数の名前付き定数または値が含まれている場合は、valueの各値、名前、またはコンマの前または後に 1 つ以上の空白文字を含めることができます。 valueがリストの場合、resultは、指定された名前または基になる値を、ビットごとにOR演算で組み合わせた値となります。 が列挙値の名前の文字列表現である場合 value 、列挙名との比較 value は パラメーターによって異なります ignoreCase 。 の場合 true、比較では大文字と小文字が区別されません。の場合 falseは大文字と小文字が区別されます。

valueTEnumの名前付き定数に対応しない名前である場合、メソッドはfalseを返します。 valueTEnum列挙体の基になる値を表さない整数の文字列表現の場合、メソッドは、基になる値が整数型に変換されたvalueである列挙体のメンバーを返します。 この動作が望ましくない場合は、IsDefinedメソッドを呼び出して、整数の特定の文字列形式が実際にTEnumのメンバーであることを確認します。

解析操作が失敗した場合、 result パラメーターには既定値の 0 が含まれます。これは、基になる TEnum 列挙体のメンバーではない可能性があります。 の TEnum名前付き定数に値が割り当てられていない場合、既定値は の最初の TEnumメンバーと等しくなります。 それ以外の場合、既定値は、割り当てられた値が 0 の列挙体のメンバーと等しくなります。

こちらもご覧ください

適用対象

TryParse<TEnum>(ReadOnlySpan<Char>, Boolean, TEnum)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。 演算で大文字と小文字を区別するかどうかをパラメーターで指定します。 戻り値は、変換が成功したかどうかを示します。

public:
generic <typename TEnum>
 where TEnum : value class static bool TryParse(ReadOnlySpan<char> value, bool ignoreCase, [Runtime::InteropServices::Out] TEnum % result);
public static bool TryParse<TEnum> (ReadOnlySpan<char> value, bool ignoreCase, out TEnum result) where TEnum : struct;
static member TryParse : ReadOnlySpan<char> * bool * 'Enum -> bool (requires 'Enum : struct)
Public Shared Function TryParse(Of TEnum As Structure) (value As ReadOnlySpan(Of Char), ignoreCase As Boolean, ByRef result As TEnum) As Boolean

型パラメーター

TEnum

result オブジェクトの型。

パラメーター

value
ReadOnlySpan<Char>

1 つ以上の列挙定数の名前または数値のスパン表現。

ignoreCase
Boolean

大文字と小文字を区別しない場合は true。大文字と小文字を区別する場合は false

result
TEnum

このメソッドが を true返す場合は、解析された値を表す列挙定数が含まれます。

戻り値

変換が成功した場合は true。それ以外の場合は false

例外

TEnum は列挙型ではありません。

.NET 8 以降のバージョン: TEnum は、ブール値に基づく列挙型です。

適用対象