當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# Type.GetEnumValues()用法及代碼示例


Type.GetEnumValues()方法用於返回當前枚舉類型中的常量值的數組。

用法:

public virtual Array GetEnumValues ();

返回值:此方法返回一個包含值的數組。數組的元素按二進製值(即枚舉常量的無符號值)排序。


異常:如果當前類型不是枚舉,則此方法將提供ArgumentException。

以下示例程序旨在說明上述方法的用法:

示例1:

// C# program to demonstrate the 
// Type.GetEnumValues() Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
class GFG { 
  
    // Defining enum ABC 
    enum ABC { A, 
               B, 
               C, 
               D, 
               E, 
               F } 
  
    // Main Method 
    public static void Main() 
    { 
  
        // try-catch block to 
        // handle exceptions 
        try { 
  
            // Creating and initializing object 
            // of ABC with instace of enum ABC 
            ABC a = ABC.A; 
  
            // Declaring and initializing 
            // object of Type 
            Type type = a.GetType(); 
  
            // Getting an array of the values  
            // of the constants 
            // By using GetEnumValues() method 
            Array obj = type.GetEnumValues(); 
  
            // Display values of the constants 
            Console.Write("Values of the constants is : {0} ", obj); 
        } 
  
        // 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); 
        } 
    } 
}
輸出:
Values of the constants is : GFG+ABC[] 

示例2:對於ArgumentException

// C# program to demonstrate the 
// Type.GetEnumValues() Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
class GFG { 
  
    // Defining enum ABC 
    enum ABC { A, 
               B, 
               C, 
               D, 
               E, 
               F } 
  
    // Main Method 
    public static void Main() 
    { 
  
        // try-catch block to 
        // handle exceptions 
        try { 
  
            // Creating and initializing object 
            // of ABC with instace of enum ABC 
            ABC a = ABC.A; 
  
            // Declaring and initializing 
            // object of Type 
            Type type = typeof(int); 
  
            // Getting an array of the values  
            // of the constants 
            // By using GetEnumValues() method 
            Array obj = type.GetEnumValues(); 
  
            // Display values of the constants 
            Console.Write("Values of the constants is : {0} ", obj); 
        } 
  
        // 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

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Type.GetEnumValues() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。