C#のType.GetEnumNames()メソッド
C#のType.GetEnumNames()メソッドは、現在の列挙型のメンバーの名前を返すために使用されます。
構文
以下は構文です-
public virtual string[] GetEnumNames ();
例
Type.GetEnumNames()メソッドを実装する例を見てみましょう-
using System;
public class Demo {
enum Vehicle {Car, Bus, Bike}
public static void Main(){
try {
Type type = typeof(string);
string[] str = type.GetEnumNames();
Console.WriteLine("GetEnumName() to return the constant name = " + str);
Console.WriteLine("Listing constants ..");
for (int i = 0; i < str.Length; i++)
Console.Write("{0} ", str[i]);
}
catch (ArgumentException e){
Console.WriteLine("Not an enum!");
Console.Write("{0}", e.GetType(), e.Message);
}
}
} 出力
これにより、次の出力が生成されます-
Not an enum! System.ArgumentException
例
Type.GetEnumNames()メソッドを実装する別の例を見てみましょう-
using System;
public class Demo {
enum Vehicle {Car, Bus, Bike, airplane}
public static void Main(){
try {
Vehicle v = Vehicle.Bike;
Type type = v.GetType();
string[] str = type.GetEnumNames();
Console.WriteLine("GetEnumName() to return the constant name = " + str);
Console.WriteLine("Listing constants ..");
for (int i = 0; i < str.Length; i++)
Console.Write("{0} ", str[i]);
}
catch (ArgumentException e){
Console.WriteLine("Not an enum!");
Console.Write("{0}", e.GetType(), e.Message);
}
}
} 出力
これにより、次の出力が生成されます-
GetEnumName() to return the constant name = System.String[] Listing constants .. Car Bus Bike airplane
-
C#のType.GetArrayRank()メソッド
C#のType.GetArrayRank()メソッドは、配列の次元数を取得します。 構文 public virtual int GetArrayRank (); Type.GetArrayRank()メソッドを実装する例を見てみましょう- 例 using System; public class Demo { public static void Main(string[] args) { Type type = typeof(int[,, ]); int arrRank = typ
-
C#のType.Equals()メソッド
C#のType.Equals()メソッドは、現在のTypeの基になるシステムタイプが、指定されたオブジェクトまたはTypeの基になるシステムタイプと同じであるかどうかを判断します。 構文 public virtual bool Equals (Type o); public override bool Equals (object o); 上記のパラメータは、基礎となるシステムタイプが現在のタイプの基礎となるシステムタイプと比較されるオブジェクトです。 Type.Equals()メソッドを実装する例を見てみましょう- using System; public class Demo { &n