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
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短