How would you rewrite this line of code without using the ?? operator and achieve functional equivalence (in terms of side effects)?
var someval = SomeMethod() ?? String.Empty;
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
How would you rewrite this line of code without using the ?? operator and achieve functional equivalence (in terms of side effects)?
var someval = SomeMethod() ?? String.Empty;
// you wouldn't want to eval twice. var test = SomeMethod(); var someval = test != null ? test : String.Empty; // or would you?
-Josh
@cbae: Maybe monad.
@JoshRoss: Yup. That is absolutely correct.
?? is a beautiful thing for that reason, yet I still this kind of code all the time:
var someval = (SomeMethod() == null) ? String.Empty : SomeMethod();
I have never heard of this operator... and now I have ![]()
From:
http://stackoverflow.com/questions/446835/what-do-two-question-marks-together-mean-in-c
It's the null coalescing operator, and quite like the ternary (immediate-if) operator.
FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();
expands to:
FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper();
which further expands to:
if(formsAuth != null)
FormsAuth = formsAuth;
else
FormsAuth = new FormsAuthenticationWrapper();
In English, it means "If whatever is to the left is not null, use that, otherwise use what's to the right."
Note that you can use any number of these in sequence. The following statement will assign the first non-null Answer# to Answer:
string Answer = Answer1 ?? Answer2 ?? Answer3 ?? Answer4;
Why have I never seen that before? It isn't like I'm new to C#.
I guess I need to catch up on all the changes...
That's a new operator to me too. It looks a lot like Oracle's NVL function.
SELECT NVL(some_col, 'null') FROM DUAL;
I've been using this operator for a while, but I always thought it was syntactic sugar. Yesterday, I decided run some code through the debugger, and I was surprised to find the difference between them.
This will invoke SomeMethod() twice:
var someval = (SomeMethod() == null) ? String.Empty : SomeMethod();
This will not:
var someval = SomeMethod() ?? String.Empty;
Resharper taught me this trick some time ago. ?? is one of my best friends. I highly recommend having a good refactoring tool like Resharper riding shotgun as it can show you little gems like this.
@cbae: I hadn't thought of that!
I've been using ?? since I started using .NET3.5 -- it's like the T-SQL ISNULL(field, defaultvalue) function.
I just assumed everyone would know about it; perhaps I should add this to our list of developer interview questions.
Herbie
I saw ?? in a code sample from a vendor about a year ago and thought I had encoding issues... now I use it all the time. In the 1.1/2.0 days I had a crappy utility library of NoZed() functions dedicated for this.
I leared this like last week. But, can the mothod return any object type with null value? Can you assign either myObject(null) and string(null) in this statement? Will it raise compiler error? I am going to try that out.
update: doesn't compile, which is good.
I work with guys I've shown things like ?? to but they still like writing statements like:
if (isDirty != null && isDirty == true){
Save()
}
Forget the adoption of ??; comparing a Boolean? They say that it's more readable. Ug...
(Ok, a nullable Boolean is a bad example but you get what I mean.)
13 seconds ago, DeathByVisualStudio wrote
I work with guys I've shown things like ?? to but they still like writing statements like:
123if(isDirty !=null&& isDirty ==true){Save()}Forget the adoption of ??; comparing a Boolean? They say that it's more readable. Ug...
(Ok, a nullable Boolean is a bad example but you get what I mean.)
Ugh is right. Although if you didn't know what ?? does, this might be a little confusing to some:
if (isDirty ?? false){ Save()}But that is when curiosity should lead you to find out WTF the ?? operator is for, and then you might actually learn something.
Add your 2¢