Export (0) Print
Expand All

Enum.GetValues Method

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

[Visual Basic]
Public Shared Function GetValues( _
   ByVal enumType As Type _
) As Array
[C#]
public static Array GetValues(
 Type enumType
);
[C++]
public: static Array* GetValues(
 Type* enumType
);
[JScript]
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.

Exceptions

Exception Type Condition
ArgumentNullException enumType is a null reference (Nothing in Visual Basic).
ArgumentException enumType is not an Enum.

Example

The following code sample illustrates the use of GetValues:

[Visual Basic] 
Imports System

Public Class GetValuesTest
    
    Enum Colors
        Red
        Green
        Blue
        Yellow
    End Enum 'Colors
    
    Enum Styles
        Plaid = 0
        Striped = 23
        Tartan = 65
        Corduroy = 78
    End Enum 'Styles
    
    Public Shared Sub Main()
        
        Console.WriteLine("The values of the Colors Enum are:")
        Dim i As Integer
        For Each i In  [Enum].GetValues(GetType(Colors))
            Console.WriteLine(i)
        Next i

        Console.WriteLine()
        
        Console.WriteLine("The values of the Styles Enum are:")
        For Each i In  [Enum].GetValues(GetType(Styles))
            Console.WriteLine(i)
        Next i
    End Sub 'Main
End Class 'GetValuesTest

[C#] 
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);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;

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

int main() {

    Console::WriteLine("The values of the Colors Enum are:");
    Array* a = Enum::GetValues(__typeof(Colors));

    for(Int32 i=0; i<a->Length; i++){
        Object* o = a->GetValue(i);
        Console::WriteLine("{0}", Enum::Format(__typeof(Colors), o, "D"));
    }

    Console::WriteLine();

    Console::WriteLine("The values of the Styles Enum are:");
    Array* b = Enum::GetValues(__typeof(Styles));
    for(Int32 i=0; i<b->Length; i++){
        Object* o = b->GetValue(i);
        Console::WriteLine("{0}", Enum::Format(__typeof(Styles), o, "D"));
    }
}


[JScript] 
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));
    }
}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Enum Class | Enum Members | System Namespace

Show:
© 2015 Microsoft