当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。