Tag: coalesce

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

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

null coalesce操作符线程是否安全?

所以这就是问题的关键:Foo.Bar可以返回null吗? 为了澄清,’_bar’在被评估为非null并且返回它之前可以设置为null吗? public class Foo { Object _bar; public Object Bar { get { return _bar ?? new Object(); } set { _bar = value; } } } 我知道使用以下get方法并不安全,并且可以返回null值: get { return _bar != null ? _bar : new Object(); } 更新: 另一种看待同一问题的方法,这个例子可能更清楚: public static T GetValue(ref T value) where T : class, new() { […]