Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: MyndFyre on December 12, 2006, 02:29 AM

Title: [C#] The ?? operator??
Post by: MyndFyre on December 12, 2006, 02:29 AM
This one got by me!

Apparently in C# 2.0 with the introduction of nullable types (Nullable<T>), C# grew an operator:


int? x = null;
int? y = x ?? 0;

?? is a binary operator that returns the left value if it is not null, otherwise the right value.

Wacky!
Title: Re: [C#] The ?? operator??
Post by: Joe[x86] on December 12, 2006, 06:16 PM
I can only thing of a single use for this:

String errorMessage404 = localizedString("error404", "es") ?? "Error 404: File not found."

I'm sure there must be some other uses, but none seem obvious.
Title: Re: [C#] The ?? operator??
Post by: MyndFyre on December 12, 2006, 08:00 PM
Quote from: Joex86] link=topic=16128.msg162518#msg162518 date=1165969018]
I can only thing of a single use for this:

String errorMessage404 = localizedString("error404", "es") ?? "Error 404: File not found."

I'm sure there must be some other uses, but none seem obvious.

......
Title: Re: [C#] The ?? operator??
Post by: Hero on December 12, 2006, 09:47 PM
Quote from: MyndFyre[vL] on December 12, 2006, 08:00 PM
Quote from: Joex86] link=topic=16128.msg162518#msg162518 date=1165969018]
I can only thing of a single use for this:

String errorMessage404 = localizedString("error404", "es") ?? "Error 404: File not found."

I'm sure there must be some other uses, but none seem obvious.

......
Exactly what I was thinking....
Title: Re: [C#] The ?? operator??
Post by: Joe[x86] on December 13, 2006, 10:17 AM
So, what did I miss?
Title: Re: [C#] The ?? operator??
Post by: MyndFyre on December 13, 2006, 05:13 PM
Quote from: Joex86] link=topic=16128.msg162554#msg162554 date=1166026656]
So, what did I miss?

Indeed, heRo, what did he miss?
Title: Re: [C#] The ?? operator??
Post by: Hero on December 13, 2006, 05:49 PM
Quote from: MyndFyre[vL] on December 13, 2006, 05:13 PM
Quote from: Joex86] link=topic=16128.msg162554#msg162554 date=1166026656]
So, what did I miss?

Indeed, heRo, what did he miss?
I never said he missed anything...
Title: Re: [C#] The ?? operator??
Post by: Warrior on December 13, 2006, 05:50 PM
I love you Myndfyre.
Title: Re: [C#] The ?? operator??
Post by: Explicit on December 13, 2006, 09:17 PM
Quote from: Warrior on December 13, 2006, 05:50 PM
I love you Myndfyre.

Me, too.
Title: Re: [C#] The ?? operator??
Post by: shout on December 14, 2006, 01:00 AM
I love myself.
Title: Re: [C#] The ?? operator??
Post by: Imperceptus on December 15, 2006, 01:18 PM
just to save you from having to type an extra colon and a null in your code? seems slightly redundant from just ?
Title: Re: [C#] The ?? operator??
Post by: MyndFyre on December 15, 2006, 08:23 PM
Quote from: Imperceptus on December 15, 2006, 01:18 PM
just to save you from having to type an extra colon and a null in your code? seems slightly redundant from just ?

string val = x ?? "null x";
string val = x == null ? "null x" : x;


I think the first is much clearer in intent.