Enum.TryParse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將一或多個列舉常數之名稱或數值的字串表示轉換為相等的列舉物件。 傳回值表示轉換是否成功。
多載
| 名稱 | Description |
|---|---|
| TryParse(Type, ReadOnlySpan<Char>, Object) |
將一或多個列舉常數之名稱或數值的字元表示範圍轉換為相等的列舉物件。 |
| TryParse(Type, String, Object) |
將一或多個列舉常數之名稱或數值的字串表示轉換為相等的列舉物件。 |
| TryParse(Type, ReadOnlySpan<Char>, Boolean, Object) |
將一或多個列舉常數之名稱或數值的字元表示範圍轉換為相等的列舉物件。 參數會指定作業是否不區分大小寫。 |
| TryParse(Type, String, Boolean, Object) |
將一或多個列舉常數之名稱或數值的字串表示轉換為相等的列舉物件。 |
| TryParse<TEnum>(ReadOnlySpan<Char>, TEnum) |
將一或多個列舉常數之名稱或數值的字串表示轉換為相等的列舉物件。 |
| TryParse<TEnum>(String, TEnum) |
將一或多個列舉常數之名稱或數值的字串表示轉換為相等的列舉物件。 傳回值表示轉換是否成功。 |
| TryParse<TEnum>(String, Boolean, TEnum) |
將一或多個列舉常數之名稱或數值的字串表示轉換為相等的列舉物件。 參數會指定作業是否區分大小寫。 傳回值表示轉換是否成功。 |
| TryParse<TEnum>(ReadOnlySpan<Char>, Boolean, TEnum) |
將一或多個列舉常數之名稱或數值的字串表示轉換為相等的列舉物件。 參數會指定作業是否區分大小寫。 傳回值表示轉換是否成功。 |
TryParse(Type, ReadOnlySpan<Char>, Object)
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
將一或多個列舉常數之名稱或數值的字元表示範圍轉換為相等的列舉物件。
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>
一個或多個列舉常數名稱或數值的張成表示法。
- result
- Object
當此方法回傳 true時,包含一個代表解析後值的枚舉常數。
傳回
true 若轉換成功; false 否則,
例外狀況
.NET 8 及以後版本:enumType 是一種布林背列舉類型。
適用於
TryParse(Type, String, Object)
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
將一或多個列舉常數之名稱或數值的字串表示轉換為相等的列舉物件。
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
一個或多個列舉常數的名稱或數值的字串表示法。
- result
- Object
當此方法回傳 true時,包含一個代表解析後值的枚舉常數。
傳回
true 若轉換成功; false 否則,
例外狀況
.NET 8 及以後版本:enumType 是一種布林背列舉類型。
適用於
TryParse(Type, ReadOnlySpan<Char>, Boolean, Object)
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
將一或多個列舉常數之名稱或數值的字元表示範圍轉換為相等的列舉物件。 參數會指定作業是否不區分大小寫。
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>
一個或多個列舉常數名稱或數值的張成表示法。
- ignoreCase
- Boolean
true 在機殼不敏感模式下閱讀 enumType ; false 在機殼敏感模式下閱讀 enumType 。
- result
- Object
當此方法回傳 true時,包含一個代表解析後值的枚舉常數。
傳回
true 若轉換成功; false 否則,
例外狀況
.NET 8 及以後版本:enumType 是一種布林背列舉類型。
適用於
TryParse(Type, String, Boolean, Object)
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
將一或多個列舉常數之名稱或數值的字串表示轉換為相等的列舉物件。
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
一個或多個列舉常數的名稱或數值的字串表示法。
- ignoreCase
- Boolean
true 在機殼不敏感模式下閱讀 value ; false 在機殼敏感模式下閱讀 value 。
- result
- Object
當此方法回傳 true時,包含一個代表解析後值的枚舉常數。
傳回
true 若轉換成功; false 否則,
例外狀況
.NET 8 及以後版本:enumType 是一種布林背列舉類型。
適用於
TryParse<TEnum>(ReadOnlySpan<Char>, TEnum)
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
將一或多個列舉常數之名稱或數值的字串表示轉換為相等的列舉物件。
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>
一個或多個列舉常數名稱或數值的張成表示法。
- result
- TEnum
當此方法回傳 true時,包含一個代表解析後值的枚舉常數。
傳回
true 若轉換成功; false 否則,
例外狀況
TEnum 不是列舉類型。
.NET 8 及以後版本:TEnum 是一種布林背列舉類型。
適用於
TryParse<TEnum>(String, TEnum)
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
將一或多個列舉常數之名稱或數值的字串表示轉換為相等的列舉物件。 傳回值表示轉換是否成功。
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
當此方法回傳時,包含一個型態TEnum為的物件,若解析操作成功,其值為 。value 若解析操作失敗, 包含底層類型 TEnum的預設值。 這個參數會未初始化傳遞。
傳回
true 若 value 參數成功轉換;否則, 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前後可有一個或多個空白。 若 value 為列表, result 則反映指定名稱或底層值的值,結合位 OR 元運算。 若 value 是列舉值名稱的字串表示,則與列舉名稱的比較 value 是區分大小寫的。
若 value 是不對應於 的 TEnum命名常數的名稱,則該方法返回 false。 若 value 是整數的字串表示,且不代表列舉的 TEnum 底層值,該方法會回傳一個枚舉成員,其底層值會 value 轉換為整數型別。 若此行為不理想,則呼叫該 IsDefined 方法以確保整數的特定字串表示實際上是 的成員 TEnum。
若解析操作失敗, result 該參數預設值為 0,且可能不是底層 TEnum 列舉的成員。 若 中 TEnum命名常數未賦予值,預設值為 的第一個成員 TEnum。 否則,預設值等於列舉中賦予值為 0 的成員。
另請參閱
適用於
TryParse<TEnum>(String, Boolean, TEnum)
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
將一或多個列舉常數之名稱或數值的字串表示轉換為相等的列舉物件。 參數會指定作業是否區分大小寫。 傳回值表示轉換是否成功。
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
當此方法回傳時,包含一個型態TEnum為的物件,若解析操作成功,其值為 。value 若解析操作失敗, 包含底層類型 TEnum的預設值。 這個參數會未初始化傳遞。
傳回
true 若 value 參數成功轉換;否則, 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前後可有一個或多個空白。 若 value 為列表, result 則反映指定名稱或底層值的值,結合位 OR 元運算。 若 value 是列舉值名稱的字串表示,則value與列舉名稱的比較取決於參數。ignoreCase 若 true,則比較不區分大小寫;若 false,則不區分大小寫。
若 value 是不對應於 的 TEnum命名常數的名稱,則該方法返回 false。 若 value 是整數的字串表示,且不代表列舉的 TEnum 底層值,該方法會回傳一個枚舉成員,其底層值會 value 轉換為整數型別。 若此行為不理想,則呼叫該 IsDefined 方法以確保整數的特定字串表示實際上是 的成員 TEnum。
若解析操作失敗, result 該參數預設值為 0,且可能不是底層 TEnum 列舉的成員。 若 中 TEnum命名常數未賦予值,預設值為 的第一個成員 TEnum。 否則,預設值等於列舉中賦予值為 0 的成員。
另請參閱
適用於
TryParse<TEnum>(ReadOnlySpan<Char>, Boolean, TEnum)
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
- 來源:
- Enum.cs
將一或多個列舉常數之名稱或數值的字串表示轉換為相等的列舉物件。 參數會指定作業是否區分大小寫。 傳回值表示轉換是否成功。
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>
一個或多個列舉常數名稱或數值的張成表示法。
- ignoreCase
- Boolean
true 忽略案件; false 以考慮案件。
- result
- TEnum
當此方法回傳 true時,包含一個代表解析後值的枚舉常數。
傳回
true 若轉換成功; false 否則,
例外狀況
TEnum 不是列舉類型。
.NET 8 及以後版本:TEnum 是一種布林背列舉類型。