Enum.TryParse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
把一或多個列舉常數的名稱或數值的字串表示轉換為一個相等列舉物件。 指出轉換是否成功的傳回值。
多載
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)
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- 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)
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- 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)
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- 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)
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- 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)
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- 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)
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- 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)
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- 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)
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- 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
是布林值支援的列舉類型。
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應