gra*_*ady 4 c# refactoring if-statement
我有这个代码:
int someValue = 100;
if (x == 5)
{
if (someCondition)
{
return someValue;
}
return someValue / 12;
}
if (x == 6)
{
if (someCondition)
{
return someValue * 12;
}
return someValue;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,someCondition始终相同,只是返回值不同.有没有办法简化这个?
让我们看看,你怎么看待这个?
int someValue = 100;
if (x == 5)
return someCondition ? someValue : (someValue / 12);
else if (x == 6)
return someCondition ? (someValue * 12) : someValue;
Run Code Online (Sandbox Code Playgroud)