C#의 Type.Equals() 메서드는 현재 Type의 기본 시스템 유형이 지정된 Object 또는 Type의 기본 시스템 유형과 동일한지 확인합니다.
구문
public virtual bool Equals (Type o); public override bool Equals (object o);
위의 매개변수는 기본 시스템 유형이 현재 유형의 기본 시스템 유형과 비교될 개체입니다.
이제 Type.Equals() 메서드를 구현하는 예를 살펴보겠습니다.
using System;
public class Demo {
public static void Main(string[] args) {
Type val1 = typeof(System.UInt16);
Type val2 = typeof(System.Int32);
Console.WriteLine("Are both the types equal? "+val1.Equals(val2));
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Are both the types equal? False
이제 Type.Equals() 메서드를 구현하는 또 다른 예를 살펴보겠습니다.
예시
using System;
using System.Reflection;
public class Demo {
public static void Main(string[] args) {
Type type = typeof(String);
Object obj = typeof(String).GetTypeInfo();
Type type2 = obj as Type;
if (type2 != null)
Console.WriteLine("Both types are equal? " +type.Equals(type2));
else
Console.WriteLine("Cannot cast!");
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Both types are equal? True