Enum.TryParse メソッド
定義
文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. 戻り値は、変換が成功したかどうかを示します。The return value indicates whether the conversion succeeded.
オーバーロード
TryParse(Type, String, Object) |
文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. |
TryParse(Type, String, Boolean, Object) |
文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. |
TryParse<TEnum>(String, Boolean, TEnum) |
文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. 演算で大文字と小文字を区別するかどうかをパラメーターで指定します。A parameter specifies whether the operation is case-sensitive. 戻り値は、変換が成功したかどうかを示します。The return value indicates whether the conversion succeeded. |
TryParse<TEnum>(String, TEnum) |
文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. 戻り値は、変換が成功したかどうかを示します。The return value indicates whether the conversion succeeded. |
TryParse(Type, String, Object)
文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.
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
解析に使用する列挙型。The enum type to use for parsing.
- value
- String
1 つ以上の列挙定数の名前または数値の文字列表現。The string representation of the name or numeric value of one or more enumerated constants.
- result
- Object
このメソッドが true
を返すとき、解析された値を表す列挙定数を格納しているオブジェクト。When this method returns true
, an object containing an enumeration constant representing the parsed value.
戻り値
変換が成功した場合は true
。それ以外の場合は false
。true
if the conversion succeeded; false
otherwise.
適用対象
TryParse(Type, String, Boolean, Object)
文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.
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
解析に使用する列挙型。The enum type to use for parsing.
- value
- String
1 つ以上の列挙定数の名前または数値の文字列表現。The string representation of the name or numeric value of one or more enumerated constants.
- ignoreCase
- Boolean
大文字と小文字を区別しないモードで value
を読み取る場合は、true
。大文字と小文字を区別するモードで value
を読み取る場合は false
。true
to read value
in case insensitive mode; false
to read value
in case sensitive mode.
- result
- Object
このメソッドが true
を返すとき、解析された値を表す列挙定数を格納しているオブジェクト。When this method returns true
, an object containing an enumeration constant representing the parsed value.
戻り値
変換が成功した場合は true
。それ以外の場合は false
。true
if the conversion succeeded; false
otherwise.
適用対象
TryParse<TEnum>(String, Boolean, TEnum)
文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. 演算で大文字と小文字を区別するかどうかをパラメーターで指定します。A parameter specifies whether the operation is case-sensitive. 戻り値は、変換が成功したかどうかを示します。The return value indicates whether the conversion succeeded.
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
の変換先の列挙型。The enumeration type to which to convert value
.
パラメーター
- value
- String
変換する列挙定数の名前または基になる値の文字列形式。The string representation of the enumeration name or underlying value to convert.
- ignoreCase
- Boolean
大文字と小文字を区別しない場合は true
。大文字と小文字を区別する場合は false
。true
to ignore case; false
to consider case.
- result
- TEnum
このメソッドから制御が戻るときに、result
には、解析操作が成功したときに値が TEnum
で表される value
型のオブジェクトが格納されます。When this method returns, result
contains an object of type TEnum
whose value is represented by value
if the parse operation succeeds. 解析操作が失敗した場合、result
には TEnum
の基になる型の既定値が格納されます。If the parse operation fails, result
contains the default value of the underlying type of TEnum
. この値が TEnum
列挙型のメンバーである必要がないことに注意してください。Note that this value need not be a member of the TEnum
enumeration. このパラメーターは初期化せずに渡されます。This parameter is passed uninitialized.
戻り値
value
パラメーターが正常に変換された場合は true
。それ以外の場合は false
。true
if the value
parameter was converted successfully; otherwise, false
.
例外
TEnum
は列挙型ではありません。TEnum
is not an enumeration type.
例
次の例では、Colors
列挙体を定義し、TryParse<TEnum>(String, Boolean, TEnum)メソッドを呼び出して文字列を対応する列挙値に変換し、IsDefinedメソッドを呼び出して、特定の整数値がColors
列挙体の基になる値であることを確認します。The following example defines a Colors
enumeration, calls the TryParse<TEnum>(String, Boolean, TEnum) method to convert strings to their corresponding enumeration values, and calls the IsDefined method to ensure that particular integral values are underlying values in the Colors
enumeration. TryParse<TEnum>(String, Boolean, TEnum)メソッドは、名前付き定数の文字列形式を等価の列挙値に変換しようとしたときに、大文字と小文字を区別しない比較を使用します。The TryParse<TEnum>(String, Boolean, TEnum) method uses case-insensitive comparison when trying to convert the string representations of named constants to their equivalent enumeration values.
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.
<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
を返します。TryParse<TEnum>(String, Boolean, TEnum) is identical to the Parse(Type, String, Boolean) method, except that instead of throwing an exception, it returns false
if the conversion fails. これにより、列挙値の文字列形式を解析するときに例外処理を行う必要がなくなります。It eliminates the need for exception handling when parsing the string representation of an enumeration value.
value
パラメーターには、列挙メンバーの基になる値または名前付き定数の文字列形式、またはコンマ (,) で区切られた名前付き定数または基になる値のリストが含まれます。The value
parameter contains the string representation of an enumeration member's underlying value or named constant, or a list of named constants or underlying values delimited by commas (,). value
に複数の名前付き定数または値が含まれている場合は、value
の各値、名前、またはコンマの前または後に 1 つ以上の空白文字を含めることができます。If value
includes multiple named constants or values, one or more blank spaces can precede or follow each value, name, or comma in value
. value
がリストの場合、result
は、指定された名前または基になる値を、ビットごとにOR
演算で組み合わせた値となります。If value
is a list, result
reflects the value of the specified names or underlying values combined with a bitwise OR
operation. value
が列挙値の名前の文字列表現である場合、 value
と列挙名の比較はパラメーターによって異なり ignoreCase
ます。If value
is the string representation of the name of an enumeration value, the comparison of value
with enumeration names depends on the ignoreCase
parameter. の場合、 true
比較では大文字と小文字が区別されません。の場合は、 false
大文字と小文字が区別されます。If true
, the comparison is case-insensitive; if false
, it is case-sensitive.
value
がTEnum
の名前付き定数に対応しない名前である場合、メソッドはfalse
を返します。If value
is a name that does not correspond to a named constant of TEnum
, the method returns false
. value
がTEnum
列挙体の基になる値を表さない整数の文字列表現の場合、メソッドは、基になる値が整数型に変換されたvalue
である列挙体のメンバーを返します。If value
is the string representation of an integer that does not represent an underlying value of the TEnum
enumeration, the method returns an enumeration member whose underlying value is value
converted to an integral type. この動作が望ましくない場合は、IsDefinedメソッドを呼び出して、整数の特定の文字列形式が実際にTEnum
のメンバーであることを確認します。 If this behavior is undesirable, call the IsDefined method to ensure that a particular string representation of an integer is actually a member of TEnum
.
こちらもご覧ください
適用対象
TryParse<TEnum>(String, TEnum)
文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. 戻り値は、変換が成功したかどうかを示します。The return value indicates whether the conversion succeeded.
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
の変換先の列挙型。The enumeration type to which to convert value
.
パラメーター
- value
- String
変換する列挙定数の名前または基になる値の文字列形式。大文字と小文字が区別されます。The case-sensitive string representation of the enumeration name or underlying value to convert.
- result
- TEnum
このメソッドから制御が戻るときに、result
には、解析操作が成功したときに値が TEnum
で表される value
型のオブジェクトが格納されます。When this method returns, result
contains an object of type TEnum
whose value is represented by value
if the parse operation succeeds. 解析操作が失敗した場合、result
には TEnum
の基になる型の既定値が格納されます。If the parse operation fails, result
contains the default value of the underlying type of TEnum
. この値が TEnum
列挙型のメンバーである必要がないことに注意してください。Note that this value need not be a member of the TEnum
enumeration. このパラメーターは初期化せずに渡されます。This parameter is passed uninitialized.
戻り値
value
パラメーターが正常に変換された場合は true
。それ以外の場合は false
。true
if the value
parameter was converted successfully; otherwise, false
.
例外
TEnum
は列挙型ではありません。TEnum
is not an enumeration type.
例
次の例では、Colors
列挙体を定義し、TryParse<TEnum>(String, TEnum)メソッドを呼び出して文字列を対応する列挙値に変換し、IsDefinedメソッドを呼び出して、特定の整数値がColors
列挙体の基になる値であることを確認します。The following example defines a Colors
enumeration, calls the TryParse<TEnum>(String, TEnum) method to convert strings to their corresponding enumeration values, and calls the IsDefined method to ensure that particular integral values are underlying values in the Colors
enumeration.
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.
<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
を返します。TryParse<TEnum>(String, TEnum) is identical to the Parse(Type, String) method, except that instead of throwing an exception, it returns false
if the conversion fails. これにより、列挙値の文字列形式を解析するときに例外処理を行う必要がなくなります。It eliminates the need for exception handling when parsing the string representation of an enumeration value.
value
パラメーターには、列挙メンバーの基になる値または名前付き定数の文字列形式、またはコンマ (,) で区切られた名前付き定数または基になる値のリストが含まれます。The value
parameter contains the string representation of an enumeration member's underlying value or named constant, or a list of named constants or underlying values delimited by commas (,). value
に複数の名前付き定数または値が含まれている場合は、value
の各値、名前、またはコンマの前または後に 1 つ以上の空白文字を含めることができます。If value
includes multiple named constants or values, one or more blank spaces can precede or follow each value, name, or comma in value
. value
がリストの場合、result
は、指定された名前または基になる値を、ビットごとにOR
演算で組み合わせた値となります。If value
is a list, result
reflects the value of the specified names or underlying values combined with a bitwise OR
operation. value
が列挙値の名前の文字列表現である場合、value
と列挙型の比較では大文字と小文字が区別されます。If value
is the string representation of the name of an enumeration value, the comparison of value
with enumeration names is case-sensitive.
value
がTEnum
の名前付き定数に対応しない名前である場合、メソッドはfalse
を返します。If value
is a name that does not correspond to a named constant of TEnum
, the method returns false
. value
がTEnum
列挙体の基になる値を表さない整数の文字列表現の場合、メソッドは、基になる値が整数型に変換されたvalue
である列挙体のメンバーを返します。If value
is the string representation of an integer that does not represent an underlying value of the TEnum
enumeration, the method returns an enumeration member whose underlying value is value
converted to an integral type. この動作が望ましくない場合は、IsDefinedメソッドを呼び出して、整数の特定の文字列形式が実際にTEnum
のメンバーであることを確認します。 If this behavior is undesirable, call the IsDefined method to ensure that a particular string representation of an integer is actually a member of TEnum
.