GetValues Method
.NET Framework Class Library
Enum..::.GetValues Method

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
Public Shared Function GetValues ( _
    enumType As Type _
) As Array
Visual Basic (Usage)
Dim enumType As Type
Dim returnValue As Array

returnValue = Enum.GetValues(enumType)
C#
[ComVisibleAttribute(true)]
public static Array GetValues(
    Type enumType
)
Visual C++
[ComVisibleAttribute(true)]
public:
static Array^ GetValues(
    Type^ enumType
)
JScript
public static function GetValues(
    enumType : Type
) : Array

Parameters

enumType
Type: System..::.Type
An enumeration type.

Return Value

Type: System..::.Array
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.
ExceptionCondition
ArgumentNullException

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

ArgumentException

enumType is not an Enum.

The following example illustrates the use of GetValues.

Visual Basic
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

        Console.WriteLine()

        Console.WriteLine("The values of the Styles Enum are:")
        For Each i In  [Enum].GetValues(GetType(Styles))
            Console.WriteLine(i)
        Next
    End Sub 
End Class 
' The example produces the following output:
'       The values of the Colors Enum are:
'       0
'       1
'       2
'       3
'       
'       The values of the Styles Enum are:
'       0
'       23
'       65
'       78

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);
    }
}
// The example produces the following output:
//       The values of the Colors Enum are:
//       0
//       1
//       2
//       3
//       
//       The values of the Styles Enum are:
//       0
//       23
//       65
//       78

Visual C++
using namespace System;
enum class Colors
{
   Red, Green, Blue, Yellow
};

enum class Styles
{
   Plaid = 0,
   Striped = 23,
   Tartan = 65,
   Corduroy = 78
};

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

   }
}
// The example produces the following output:
//       The values of the Colors Enum are:
//       0
//       1
//       2
//       3
//       
//       The values of the Styles Enum are:
//       0
//       23
//       65
//       78

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));
    }
}
// The example produces the following output:
//       The values of the Colors Enum are:
//       0
//       1
//       2
//       3
//       
//       The values of the Styles Enum are:
//       0
//       23
//       65
//       78

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
Page view tracker