C#中的Type.Equals()方法
C#中的Type.Equals()方法确定当前Type的基础系统类型是否与指定的Object或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("无法投射!");
}
}输出结果
这将产生以下输出-
Both types are equal? True
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短