Export (0) Print
Expand All

Enum.GetValues Method

Retrieves an array of the values of the constants in a specified enumeration.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)] 
public static Array GetValues (
	Type enumType
)
/** @attribute ComVisibleAttribute(true) */ 
public static Array GetValues (
	Type enumType
)
ComVisibleAttribute(true) 
public static function GetValues (
	enumType : Type
) : Array
Not applicable.

Parameters

enumType

An enumeration type.

Return Value

An Array of the values of the constants in enumType. The elements of the array are sorted by the binary values of the enumeration constants.

Exception typeCondition

ArgumentNullException

enumType is a null reference (Nothing in Visual Basic).

ArgumentException

enumType is not an Enum.

The following example illustrates the use of GetValues.

using System;

public class GetValuesTest {
    enum Colors { Red, Green, Blue, Yellow };
    enum Styles { Plaid = 0, Striped = 23, Tartan = 65, Corduroy = 78 };

    public static void Main() {

        Console.WriteLine("The values of the Colors Enum are:");
        foreach(int i in Enum.GetValues(typeof(Colors)))
            Console.WriteLine(i);

        Console.WriteLine();

        Console.WriteLine("The values of the Styles Enum are:");
        foreach(int i in Enum.GetValues(typeof(Styles)))
            Console.WriteLine(i);
    }
}

import System.*;

public class GetValuesTest
{
    enum Colors
    {
        red (0),
        green (1),
        blue (2),
        yellow (3);
    } //Colors

    enum Styles
    {
        plaid (0),
        striped (23),
        tartan (65),
        corduroy (78);
    } //Styles

    public static void main(String[] args)
    {
        Console.WriteLine("The values of the Colors Enum are:");
        
        Array a = Enum.GetValues(Colors.class.ToType());
        for (int iCtr = 0; iCtr < a.get_Length(); iCtr++) {
            Int32 i =( Int32)a.GetValue(iCtr);
            Console.WriteLine(i);
        }
        Console.WriteLine();

        Console.WriteLine("The values of the Styles Enum are:");
        Array a1 = Enum.GetValues(Styles.class.ToType());
        for (int iCtr = 0; iCtr < a1.get_Length(); iCtr++) {
            Int32 i =( Int32)a1.GetValue(iCtr);
            Console.WriteLine(i);
        }
    } //main
} //GetValuesTest

import System;

public class GetValuesTest {
    enum Colors { Red, Green, Blue, Yellow };
    enum Styles { Plaid = 0, Striped = 23, Tartan = 65, Corduroy = 78 };

    public static function Main() {

        Console.WriteLine("The values of the Colors Enum are:");
        for(var i : int in Enum.GetValues(Colors))
            Console.WriteLine(Enum.GetValues(Colors).GetValue(i));

        Console.WriteLine();

        Console.WriteLine("The values of the Styles Enum are:");
        for(var j : int in Enum.GetValues(Styles))
            Console.WriteLine(Enum.GetValues(Styles).GetValue(j));
    }
}

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft