我在 resharper 中有一条规则来查找对 Nullable.HasValue 的调用
T? foo;
//...
if(foo.HasValue)
{}
//And it offers to replace with a comparison directly with null:
if(foo != null)
{}
这很有效,但是当它遇到否定时.HasValue
,结果有点奇怪。
if(!foo.HasValue) {}
//is replaced with
if(!(foo != null)) {}
然后 resharper 希望我将语句简化为if(foo == null)
//ideally it would automatically get to this without the extra step:
if(foo == null) {}
该规则定义为:
type: System.ValueType or derived
nullable: expression of type System.Nullable<$type$>
search pattern:
$nullable$.HasValue
replace pattern:
$nullable$ != null
(“替换后格式”和“缩短引用”均已选中)
有没有办法编写此规则以便 ReSharper 智能地处理它?我尝试制定第二条规则!$nullable$.HasValue
,但这会导致两个规则匹配,这使得工具提示建议看起来令人困惑:replace with == null
and replace with != null
.
如果不是真正强制性的,您可以放弃此规则,因为根据此post https://stackoverflow.com/questions/676078/which-is-preferred-nullable-hasvalue-or-nullable-null:
“编译器将空比较替换为对 HasValue 的调用,因此没有真正的区别。只需执行对您和您的同事来说更具可读性/更有意义的操作即可。”
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)