是什么的typeof和关键字是区别? [英] What is the difference between typeof and the is keyword?
本文介绍了是什么的typeof和关键字是区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是两者之间的具体区别
//调用此方法时与GetByType< MyClass的>()
公共BOOL GetByType< T>(){
//返回true:
返回的typeof(T).Equals(typeof运算(MyClass的));
//这个返回false:
返回的typeof(T)是MyClass的;
}
解决方案
您应该使用是ACLASS
上的实例,而不是比较类型:
VAR将myInstance =新ACLASS( );
VAR ISIT =将myInstance是ACLASS; //真
是
还与基地 - 课程和接口:
MemoryStream的流=新的MemoryStream();
布尔isStream =流流; //真
布尔isIDispo =流IDisposable的; //真
What's the exact difference between the two?
// When calling this method with GetByType<MyClass>()
public bool GetByType<T>() {
// this returns true:
return typeof(T).Equals(typeof(MyClass));
// this returns false:
return typeof(T) is MyClass;
}
解决方案
You should use is AClass
on instances and not to compare types:
var myInstance = new AClass();
var isit = myInstance is AClass; //true
is
works also with base-classes and interfaces:
MemoryStream stream = new MemoryStream();
bool isStream = stream is Stream; //true
bool isIDispo = stream is IDisposable; //true
这篇关于是什么的typeof和关键字是区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文