C# Enum GetValues

Description

Enum GetValues retrieves an array of the values of the constants in a specified enumeration.

Syntax

Enum.GetValues has the following syntax.


[ComVisibleAttribute(true)]
public static Array GetValues(
  Type enumType
)

Parameters

Enum.GetValues has the following parameters.

  • enumType - An enumeration type.

Returns

Enum.GetValues method returns An array that contains the values of the constants in enumType.

Example

The following example displays information about the array returned by the GetValues method for an enumeration that includes a negative value, zero, and a positive value.


using System;//ww w  . j a  va  2s . c om

enum SignMagnitude { Negative = -1, Zero = 0, Positive = 1 };

public class Example
{
   public static void Main()
   {
      foreach (var value in Enum.GetValues(typeof(SignMagnitude))) {
         Console.WriteLine("{0,3}     0x{0:X8}     {1}",
                           (int) value, ((SignMagnitude) value));
      }   
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version