问题:
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
来源:oschina
链接:https://my.oschina.net/stackoom/blog/4304441