C#のType.GetEnumUnderlyingType()メソッド
C#のType.GetEnumUnderlyingType()メソッドは、現在の列挙型の基になる型を返すために使用されます。
構文
以下は構文です-
public virtual Type GetEnumUnderlyingType ();
例
Type.GetEnumUnderlyingType()メソッドを実装する例を見てみましょう-
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);
Type type2 = type.GetEnumUnderlyingType();
Console.Write("Enum Underlying type = "+type2);
Console.WriteLine("\nListing 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[] Enum Underlying type = System.Int32 Listing constants .. Car Bus Bike Airplane
例
Type.GetEnumUnderlyingType()メソッドを実装する別の例を見てみましょう-
using System;
public class Demo {
enum Vehicle {Car, Bus, Bike, Airplane}
public static void Main(){
try {
Type type = typeof(int);
string[] str = type.GetEnumNames();
Console.WriteLine("GetEnumName() to return the constant name = " + str);
Type type2 = type.GetEnumUnderlyingType();
Console.Write("Enum Underlying type = "+type2);
Console.WriteLine("\nListing 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
-
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