问题
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:
remove this useless assignment to local variable
stopFyon
回答1:
Don't use var
:
StopFYON stopFyon;
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 you would use stopFyon
before the if
the warning would also disappear.
回答2:
The message is pretty straightforward:
IEnumerable<CarOnline> carOnlineData = (IEnumerable<CarOnline>)vehrep.GetCarOnlineDetail(maintainStopFactoryOrderNo.VehicleDetail).Result;
StopFYON stopFyon;
if (carOnlineData.Any())
stopFyon = vehtran.CreateStopFactoryOrderNo(carOnlineData, maintainStopFactoryOrderNo, lastUpdatedBy);
else
stopFyon = vehtran.CreateStopFactoryOrderNo(null, maintainStopFactoryOrderNo, lastUpdatedBy);
The first assignment:
var stopFyon = new StopFYON();
is useless since another assignment is performed within the if statement
right after.
回答3:
It is working, when I change the code StopFYON stopFyon = null
instead of var stopFyon = new StopFYON();
来源:https://stackoverflow.com/questions/47903201/remove-this-useless-assignment-to-local-variable-c-sharp