Null-coalescing operator - Which of the following do you prefer? [duplicate]
-
12-11-2019 - |
سؤال
Possible Duplicate:
In your opinion what is more readable: ?? (operator) or use of if's
Let's say you have a method that checks is a list is null. If so, it instantiates it.
Generally, we'd do this:
if (StringList == null)
StringList = new List<string>();
I've recently learned about null-coalescing operator and am tempted to change that to:
StringList = StringList ?? new List<string>();
It's more compact and looks like it'll be compiled to the same set of instructions as the 'traditional' method.
Probably a bit pedantic, but I was wondering if you have any reservations against such a method and which you'd actually use.
لا يوجد حل صحيح
لا تنتمي إلى StackOverflow