删除对局部变量c#的无用分配 [英] remove this useless assignment to local variable c#

查看:663
本文介绍了删除对局部变量c#的无用分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var  stopFyon = new StopFYON();
IEnumerable<CarOnline> carOnlineData = (IEnumerable<CarOnline>)vehrep.GetCarOnlineDetail(maintainStopFactoryOrderNo.VehicleDetail).Result;

if (carOnlineData.Any())
{
    stopFyon = vehtran.CreateStopFactoryOrderNo(carOnlineData, maintainStopFactoryOrderNo, lastUpdatedBy);
}
else
{
    stopFyon = vehtran.CreateStopFactoryOrderNo(null, maintainStopFactoryOrderNo, lastUpdatedBy);
}
return gen.GetResponse((Int16)ResultCode.Success, (Int16)MsgType.Ok, null, vehrep.StopFactoryOrderNo(stopFyon));

使用声纳代码分析时出现警告错误:

I got warning error when using sonar code analysis:

删除对局部变量stopFyon

推荐答案

请勿使用var:

StopFYON stopFyon;

警告的原因是您使用默认构造函数初始化了变量(理论上这可能是一个非常昂贵的调用,至少会造成混淆).但是此分配将在所有分支(ifelse)中被覆盖.所以这没用.

The reason for the warning is that you initialize the variable with the default constructor(which could theoretically be a very expensive call, at least it's confusing). But this assignment gets overridden in all branches(the if and the else). So it's useless.

如果您要在if之前使用stopFyon,警告也会消失.

If you would use stopFyon before the if the warning would also disappear.

这篇关于删除对局部变量c#的无用分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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