小编use*_*403的帖子

在您看来,什么更具可读性?(运营商)或使用if

我有一个方法会收到一个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)

在您看来,什么是最好的选择?

c# refactoring coalesce null-coalescing-operator

3
推荐指数
2
解决办法
347
查看次数