嗯?如果真实) [英] Eh??? if(true)

查看:77
本文介绍了嗯?如果真实)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我只是使用ReSharper重构一些旧代码,建议我更改此代码:

Hi All,
I''m just refactoring some old code using ReSharper and it''s suggested I change this:

var fileinfo = new FileInfo(file);
if(fileinfo != null)
{
}



对此:



to this:

var fileinfo = new FileInfo(file)
if(true)
{
}



我不确定我是否明白这一点,我现在有一个愚蠢的时刻,但这实际上在评估什么呢?缺少对对象的明确引用使我有些困惑.

谢谢,

Jammer



I''m not sure I get this, I''m having a stupid moment but what is this actually evaluating? The lack of explicit reference to an object has confused me a bit.

Thanks,

Jammer

推荐答案

向变量分配new对象几乎总是意味着该变量为非null,这使得if评估毫无意义.如果创建这样的对象可能会失败,则正确的方法是沿
使用静态方法
Assigning a new object to a variable almost always means that the variable is non-null, which makes the if evaluation meaningless. If creating an object like this might fail, the proper way is to use a static method along the lines of
var fileinfo = FileInfo.CreateNew(file);


我''我和你在一起.
I''m with you on this.
  1. if (true)真是愚蠢.
  2. 如果某天某人会在两行之间插入fileinfo=null;怎么办?
  1. if (true) is pretty silly.
  2. What if some day, someone will insert fileinfo=null; between the two lines?


:)


这篇关于嗯?如果真实)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆