Enum.TryParse Enum.TryParse Enum.TryParse Enum.TryParse Method

定義

把一或多個列舉常數的名稱或數值的字串表示轉換為一個相等列舉物件。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, Boolean, Object) TryParse(Type, String, Boolean, Object) TryParse(Type, String, Boolean, Object) TryParse(Type, String, Boolean, Object)
TryParse(Type, String, Object) TryParse(Type, String, Object) TryParse(Type, String, Object) TryParse(Type, String, Object)
TryParse<TEnum>(String, Boolean, TEnum) TryParse<TEnum>(String, Boolean, TEnum) TryParse<TEnum>(String, Boolean, TEnum) TryParse<TEnum>(String, Boolean, TEnum)

把一或多個列舉常數的名稱或數值的字串表示轉換為一個相等列舉物件。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) TryParse<TEnum>(String, TEnum) TryParse<TEnum>(String, TEnum) TryParse<TEnum>(String, TEnum)

把一或多個列舉常數的名稱或數值的字串表示轉換為一個相等列舉物件。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, Boolean, Object) TryParse(Type, String, Boolean, Object) TryParse(Type, String, Boolean, Object) TryParse(Type, String, Boolean, 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);
static member TryParse : Type * string * bool *  -> bool
Public Shared Function TryParse (enumType As Type, value As String, ignoreCase As Boolean, ByRef result As Object) As Boolean

參數

enumType
Type Type Type Type

傳回

TryParse(Type, String, Object) TryParse(Type, String, Object) TryParse(Type, String, Object) TryParse(Type, String, 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);
static member TryParse : Type * string *  -> bool
Public Shared Function TryParse (enumType As Type, value As String, ByRef result As Object) As Boolean

參數

enumType
Type Type Type Type

傳回

TryParse<TEnum>(String, Boolean, TEnum) TryParse<TEnum>(String, Boolean, TEnum) TryParse<TEnum>(String, Boolean, TEnum) TryParse<TEnum>(String, Boolean, TEnum)

把一或多個列舉常數的名稱或數值的字串表示轉換為一個相等列舉物件。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;
static member TryParse : string * bool *  -> 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 String String String

要轉換之列舉名稱或基礎值的字串表示。The string representation of the enumeration name or underlying value to convert.

ignoreCase
Boolean Boolean Boolean Boolean

true 以忽略大小寫,false 以區分大小寫。true to ignore case; false to consider case.

result
TEnum TEnum TEnum 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.

傳回

如果 true 參數轉換成功,則為 value,否則為 falsetrue 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包含多個名為常數或值,一或多個空格可以之前或之後每個值、 名稱或在逗號valueIf 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,則方法會傳回falseIf 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方法,以確保特定的字串表示的整數是實際的成員TEnumIf 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) TryParse<TEnum>(String, TEnum) TryParse<TEnum>(String, TEnum) TryParse<TEnum>(String, TEnum)

把一或多個列舉常數的名稱或數值的字串表示轉換為一個相等列舉物件。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;
static member TryParse : string *  -> 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 String String String

要轉換之列舉名稱或基礎值之區分大小寫的字串表示。The case-sensitive string representation of the enumeration name or underlying value to convert.

result
TEnum TEnum TEnum 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.

傳回

如果 true 參數轉換成功,則為 value,否則為 falsetrue 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包含多個名為常數或值,一或多個空格可以之前或之後每個值、 名稱或在逗號valueIf 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,則方法會傳回falseIf 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方法,以確保特定的字串表示的整數是實際的成員TEnumIf this behavior is undesirable, call the IsDefined method to ensure that a particular string representation of an integer is actually a member of TEnum.

另請參閱

適用於