Type.IsEnumDefined(Object)方法用於返回一個值,該值指示當前枚舉類型中是否存在指定的值。
用法: public virtual bool IsEnumDefined (object value);
Here, it takes the value to be tested.
返回值:如果指定的值是當前枚舉類型的成員,則此方法返回true,否則返回false。
異常:
- ArgumentException:如果當前類型不是枚舉。
- ArgumentNullException:如果值為null。
- InvalidOperationException:如果值的類型不能為枚舉的基礎類型。
以下示例程序旨在說明Type.IsEnumDefined(Object)方法的用法:
示例1:
// C# program to demonstrate the
// Type.IsEnumDefined(Object) Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// defining enum enu
public enum enu {A, B, C};
// Main Method
public static void Main()
{
// try-catch block for handling Exception
try {
// creating and initializing object Type
object value = enu.A;
// checking for the current enumeration type
// by using IsEnumDefined() Method
bool status = typeof(enu).IsEnumDefined(value);
// display the result
if (status)
Console.WriteLine("specified value is a member"+
" of the current enumeration type");
else
Console.WriteLine("specified value is not"+
" present in the current enumeration type");
}
// catch ArgumentNullException here
catch (ArgumentNullException e) {
Console.WriteLine("Object is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
// catch ArgumentNullException here
catch (InvalidOperationException e) {
Console.WriteLine("Object is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
// catch ArgumentException here
catch (ArgumentException e) {
Console.WriteLine("Object is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
specified value is a member of the current enumeration type
示例2:
// C# program to demonstrate the
// Type.IsEnumDefined(Object) Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// defining enum enu
public enum enu {A, B, C};
// Main Method
public static void Main()
{
// try-catch block for handling Exception
try {
// creating and initializing object Type
object value = enu.B;
// checking for the current enumeration type
// by using IsEnumDefined() Method
bool status = typeof(int).IsEnumDefined(value);
// display the result
if (status)
Console.WriteLine("specified value is a member"+
" of the current enumeration type");
else
Console.WriteLine("specified value is not"+
" present in the current enumeration type");
}
// catch ArgumentNullException here
catch (ArgumentNullException e) {
Console.WriteLine("value is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
// catch ArgumentException here
catch (ArgumentException e) {
Console.WriteLine("The current type is not an enumeration.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
The current type is not an enumeration. Exception Thrown: System.ArgumentException
示例3:
// C# program to demonstrate the
// Type.IsEnumDefined(Object) Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// defining enum enu
public enum enu {A, B, C};
// Main Method
public static void Main()
{
// try-catch block for handling Exception
try {
// creating and initializing object Type
object value = null;
// checking for the current enumeration type
// by using IsEnumDefined() Method
bool status = typeof(enu).IsEnumDefined(value);
// display the result
if (status)
Console.WriteLine("specified value is a member"+
" of the current enumeration type");
else
Console.WriteLine("specified value is not"+
" present in the current enumeration type");
}
// catch ArgumentNullException here
catch (ArgumentNullException e) {
Console.WriteLine("value is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
// catch ArgumentException here
catch (ArgumentException e) {
Console.WriteLine("The current type is not an enumeration.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
value is null. Exception Thrown: System.ArgumentNullException
參考:
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Type.IsEnumDefined() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。