Enum.GetNames 方法
检索指定枚举中常数名称的数组。
**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
<ComVisibleAttribute(True)> _
Public Shared Function GetNames ( _
enumType As Type _
) As String()
用法
Dim enumType As Type
Dim returnValue As String()
returnValue = Enum.GetNames(enumType)
[ComVisibleAttribute(true)]
public static string[] GetNames (
Type enumType
)
[ComVisibleAttribute(true)]
public:
static array<String^>^ GetNames (
Type^ enumType
)
/** @attribute ComVisibleAttribute(true) */
public static String[] GetNames (
Type enumType
)
ComVisibleAttribute(true)
public static function GetNames (
enumType : Type
) : String[]
参数
- enumType
枚举类型。
返回值
enumType 的常数名称的字符串数组。
异常
异常类型 | 条件 |
---|---|
enumType 为 空引用(在 Visual Basic 中为 Nothing)。 |
|
enumType 参数不是 Enum。 |
备注
返回值数组的元素按枚举常量的值排序。如果存在具有相同值的枚举常量,则不指定其相应名称的顺序。
示例
下面的代码示例说明如何使用 GetNames:
Imports System
Public Class GetNamesTest
Enum Colors
Red
Green
Blue
Yellow
End Enum 'Colors
Enum Styles
Plaid
Striped
Tartan
Corduroy
End Enum 'Styles
Public Shared Sub Main()
Console.WriteLine("The values of the Colors Enum are:")
Dim s As String
For Each s In [Enum].GetNames(GetType(Colors))
Console.WriteLine(s)
Next s
Console.WriteLine()
Console.WriteLine("The values of the Styles Enum are:")
For Each s In [Enum].GetNames(GetType(Styles))
Console.WriteLine(s)
Next s
End Sub 'Main
End Class 'GetNamesTest
using System;
public class GetNamesTest {
enum Colors { Red, Green, Blue, Yellow };
enum Styles { Plaid, Striped, Tartan, Corduroy };
public static void Main() {
Console.WriteLine("The values of the Colors Enum are:");
foreach(string s in Enum.GetNames(typeof(Colors)))
Console.WriteLine(s);
Console.WriteLine();
Console.WriteLine("The values of the Styles Enum are:");
foreach(string s in Enum.GetNames(typeof(Styles)))
Console.WriteLine(s);
}
}
using namespace System;
enum class Colors
{
Red, Green, Blue, Yellow
};
enum class Styles
{
Plaid, Striped, Tartan, Corduroy
};
int main()
{
Console::WriteLine( "The values of the Colors Enum are:" );
Array^ a = Enum::GetNames( Colors::typeid );
Int32 i = 0;
do
{
Object^ o = a->GetValue( i );
Console::WriteLine( o->ToString() );
}
while ( ++i < a->Length );
Console::WriteLine();
Console::WriteLine( "The values of the Styles Enum are:" );
Array^ b = Enum::GetNames( Styles::typeid );
i = 0;
do
{
Object^ o = b->GetValue( i );
Console::WriteLine( o->ToString() );
}
while ( ++i < b->Length );
}
import System.*;
public class GetNamesTest
{
enum Colors
{
red (0),
green (1),
blue (2),
yellow (3);
} //Colors
enum Styles
{
plaid (0),
striped (1),
tartan (2),
corduroy (3);
} //Styles
public static void main(String[] args)
{
Console.WriteLine("The values of the Colors Enum are:");
String s[] = Enum.GetNames(Colors.class.ToType());
for (int iCtr = 0; iCtr < s.length; iCtr++) {
Console.WriteLine(s[iCtr]);
}
Console.WriteLine();
Console.WriteLine("The values of the Styles Enum are:");
String s1[] = Enum.GetNames(Styles.class.ToType());
for (int iCtr = 0; iCtr < s.length; iCtr++) {
Console.WriteLine(s1[iCtr]);
}
} //main
} //GetNamesTest
import System;
public class GetNamesTest {
enum Colors { Red, Green, Blue, Yellow };
enum Styles { Plaid, Striped, Tartan, Corduroy };
public static function Main() {
Console.WriteLine("The values of the Colors Enum are:");
for(var i : int in Enum.GetNames(Colors))
Console.WriteLine(Enum.GetNames(Colors).GetValue(i));
Console.WriteLine();
Console.WriteLine("The values of the Styles Enum are:");
for(var j : int in Enum.GetNames(Styles))
Console.WriteLine(Enum.GetNames(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
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0