In your opinion what is more readable: ?? (operator) or use of if's

后端 未结 13 1725
谎友^
谎友^ 2021-01-21 07:05

I have a method that will receive a string, but before I can work with it, I have to convert it to int. Sometimes it can be null and I ha

13条回答
  •  说谎
    说谎 (楼主)
    2021-01-21 07:21

    I definitely prefer the null coalesce operator (??) over a series of if statements. Especially when you need to coalesce more than one value, the operator approach is FAR more readable. This plays into other newer features of C#, such as lambda expressions, LINQ sugar syntax, etc. The less code there is to muddy up the actual intentful code, the clearer the intent should/will be.

提交回复
热议问题