【发布时间】:2023-03-10 11:48:02
【问题描述】:
我习惯于看到类似的旧代码
if (true)
{
...
}
很明显,有人在进行更改时要么是懒惰,要么是过于谨慎。我今天遇到了这个sn-p,我很好奇这样进行类型比较是否存在功能差异:
private static bool logField(Type t, string fieldname)
{
if (t.ToString() == typeof (Property).ToString())
{
...
}
return true;
}
并这样做:
private static bool logField(Type t, string fieldname)
{
if (t == typeof (Property))
{
...
}
return true;
}
【问题讨论】:
-
我不知道有什么好的理由这样做。事实上,我认为这是一个错误,因为它至少存在潜在的 NullReferenceException 问题。