C# 中的 Type.Equals() 方法
C# 中的 Type.Equals() 方法确定当前 Type 的底层系统类型是否与指定的对象或类型的底层系统类型相同。
语法
public virtual bool Equals (Type o); public override bool Equals (object o);
以上,参数是其底层系统类型要与当前 Type 的底层系统类型进行比较的对象。
我们现在来看一个示例,实现 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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP