Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

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

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 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 code sample 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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.