C#의 Object.GetTypeCode() 메서드는 현재 인스턴스의 유형을 가져오는 데 사용됩니다.
구문
구문은 다음과 같습니다 -
public Type GetType ();
예
using System;
public class Demo {
public static void Main() {
Object ob = new Object();
String str = "Jim";
Type type1 = ob.GetType();
Type type2 = str.GetType();
Console.WriteLine("Type = "+type1);
Console.WriteLine("Type = "+type2);
Console.WriteLine("\nHash Code = "+type1.GetHashCode());
Console.WriteLine("Hash Code = "+type2.GetHashCode());
}
} 출력
Type = System.Object Type = System.String Hash Code = 30015890 Hash Code = 21083178
예
using System;
public struct Value {
private int v1;
private int v2;
private int v3;
public Value(int v1, int v2, int v3) {
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
}
public override int GetHashCode() {
return Tuple.Create(v1, v2, v3).GetHashCode();
}
}
public class Demo {
public static void Main() {
Value v = new Value(1, 7, 12);
Console.WriteLine(v.GetHashCode());
Console.WriteLine(v.GetType());
v = new Value(12, 8, 7);
Console.WriteLine(v.GetHashCode());
v = new Value(8, 7, 12);
Console.WriteLine(v.GetHashCode());
}
} 출력
1258 Value 12803 8931