共用方式為


Enum.GetValues Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Shared Function GetValues ( _
    enumType As Type _
) As Array
[ComVisibleAttribute(true)]
public static Array GetValues(
    Type enumType
)

Parameters

Return Value

Type: System.Array
An array that contains the values of the constants in enumType. The elements of the array are sorted by the binary values of the enumeration constants.

Exceptions

Exception Condition
ArgumentNullException

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

ArgumentException

enumType is not an Enum.

Remarks

The elements of the array are sorted by the binary values of the enumeration constants (that is, by their unsigned magnitude).

Examples

The following example illustrates the use of the GetValues method.


Public Class Example

   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 Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text &= "The values of the Colors Enum are:" & vbCrLf
      Dim i As Integer
      For Each i In [Enum].GetValues(GetType(Colors))
         outputBlock.Text &= i & vbCrLf
      Next i

      outputBlock.Text &= vbCrLf

      outputBlock.Text &= "The values of the Styles Enum are:" & vbCrLf
      For Each i In [Enum].GetValues(GetType(Styles))
         outputBlock.Text &= i & vbCrLf
      Next i
   End Sub 'Main
End Class 'GetValuesTest
using System;

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

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      outputBlock.Text += "The values of the Colors Enum are:" + "\n";
      foreach (int i in Enum.GetValues(typeof(Colors)))
         outputBlock.Text += i + "\n";

      outputBlock.Text += "\n";

      outputBlock.Text += "The values of the Styles Enum are:" + "\n";
      foreach (int i in Enum.GetValues(typeof(Styles)))
         outputBlock.Text += i + "\n";
   }
}

Version Information

Silverlight

Supported in: 5

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference