类型检查:typeof,GetType还是?

耗尽温柔 提交于 2020-08-16 11:00:52

问题:

I've seen many people use the following code: 我见过很多人使用以下代码:

Type t = typeof(obj1);
if (t == typeof(int))
    // Some code here

But I know you could also do this: 但我知道您也可以这样做:

if (obj1.GetType() == typeof(int))
    // Some code here

Or this: 或这个:

if (obj1 is int)
    // Some code here

Personally, I feel the last one is the cleanest, but is there something I'm missing? 就个人而言,我觉得最后一个是最干净的,但是我缺少什么吗? Which one is the best to use, or is it personal preference? 哪一种是最佳使用方式,还是个人喜好?


解决方案:

参考一: https://stackoom.com/question/47jK/类型检查-typeof-GetType还是
参考二: https://oldbug.net/q/47jK/Type-Checking-typeof-GetType-or-is
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!