我有一个方法会收到一个string,但在我可以使用它之前,我必须将其转换为int.有时它可以null,我必须改变它的价值"0".今天我有:
public void doSomeWork(string value)
{
int SomeValue = int.Parse(value ?? "0"); //it can throw an exception(i know)
}
Run Code Online (Sandbox Code Playgroud)
我做到了,但我的老板让我重构它:
public void doSomeWork(string value)
{
if(string.IsNullOrEmpty(value))
value = "0";
int SomeValue = int.Parse(value);
}
Run Code Online (Sandbox Code Playgroud)
在您看来,什么是最好的选择?