471,334 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 software developers and data experts.

Null Coalescing ?? Operator

Hi

I do like C# 2's new ?? operator, which enables us to code something like:

// Ensure someString is not null
someString = someString ?? "";

But wouldn't it make a lot of sense to take this a step further and allow a
shorthand-assignment equivalent (much like +=, -= and so on)? I.e instead of
what we have above, we could just say:

someString ??= "";

Now that would be nice...

Rich
Jan 20 '06 #1
9 1869
Me
What exactly does the new ?? operator do anyway?
"Rich" <Ri**@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Hi

I do like C# 2's new ?? operator, which enables us to code something like:

// Ensure someString is not null
someString = someString ?? "";

But wouldn't it make a lot of sense to take this a step further and allow
a
shorthand-assignment equivalent (much like +=, -= and so on)? I.e instead
of
what we have above, we could just say:

someString ??= "";

Now that would be nice...

Rich

Jan 20 '06 #2
Me <me@home.com> wrote:
What exactly does the new ?? operator do anyway?


See http://www.pobox.com/~skeet/csharp/c.../nullable.html

I hadn't planned on making the pages public yet, as they're not
finished - I'm half way through delegates, and haven't done generics or
iterators yet. However, fortunately I did nullable types yesterday...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 20 '06 #3
Rich,

But what would it do? It would only set the variable if the variable is
null?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rich" <Ri**@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Hi

I do like C# 2's new ?? operator, which enables us to code something like:

// Ensure someString is not null
someString = someString ?? "";

But wouldn't it make a lot of sense to take this a step further and allow
a
shorthand-assignment equivalent (much like +=, -= and so on)? I.e instead
of
what we have above, we could just say:

someString ??= "";

Now that would be nice...

Rich

Jan 21 '06 #4
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
But what would it do? It would only set the variable if the variable is
null?


Presumably so. You'd do something like:

s ??= someValue1;
// More code
s ??= someValue2;
// More code
s ??= someValue3;

and the final value of s would be the first non-null value used.

I'm not sure I like it quite enough to think it's worth adding as an
operator, but I'm sure there are some situations in which is would be
useful.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 21 '06 #5
If there is a use, I can't see it. I mean, are we really THAT lazy
where we need a whole new operator (and reams of code to support it in the
compiler) for one single word?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
But what would it do? It would only set the variable if the variable
is
null?


Presumably so. You'd do something like:

s ??= someValue1;
// More code
s ??= someValue2;
// More code
s ??= someValue3;

and the final value of s would be the first non-null value used.

I'm not sure I like it quite enough to think it's worth adding as an
operator, but I'm sure there are some situations in which is would be
useful.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 21 '06 #6
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
If there is a use, I can't see it. I mean, are we really THAT lazy
where we need a whole new operator (and reams of code to support it in the
compiler) for one single word?


Couldn't the same be applied to +=, -= etc too? It's only a single word
each time...

(Actually, there's a difference between x += y and x = x + y, I know,
but arguably that subtlety is confusing anyway.)

There are lots of bits of syntactic sugar which aren't really
necessary. I suspect this one is a step too far, but I'll keep an eye
out for where I *would* use it...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 21 '06 #7
string foo = null;
return foo ?? "foo was null";

equals to

string foo = null;
return foo == null ? "foo was null" : foo;
"Me" <me@home.com> wrote in message
news:R5******************************@comcast.com. ..
What exactly does the new ?? operator do anyway?
"Rich" <Ri**@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Hi

I do like C# 2's new ?? operator, which enables us to code something
like:

// Ensure someString is not null
someString = someString ?? "";

But wouldn't it make a lot of sense to take this a step further and allow
a
shorthand-assignment equivalent (much like +=, -= and so on)? I.e
instead of
what we have above, we could just say:

someString ??= "";

Now that would be nice...

Rich


Jan 21 '06 #8
<snip Nicholas>
Couldn't the same be applied to +=, -= etc too? It's only a single word
each time...

(Actually, there's a difference between x += y and x = x + y, I know,
but arguably that subtlety is confusing anyway.)

<snip Jon>

Yes, there's a difference between x += y and x = x + y, but what about
the ever present x++ (3chars) instead of x += 1 (4 chars). And the
second one is more "useful" since it could be x += any number.

Scott
Jan 23 '06 #9
Scott C wrote:
<snip Nicholas>
Couldn't the same be applied to +=, -= etc too? It's only a single word
each time...

(Actually, there's a difference between x += y and x = x + y, I know,
but arguably that subtlety is confusing anyway.)

<snip Jon>

Yes, there's a difference between x += y and x = x + y, but what about
the ever present x++ (3chars) instead of x += 1 (4 chars). And the
second one is more "useful" since it could be x += any number.


Indeed. So, the question is - where does the line get drawn? I think
it's probably too early to say how often the null coalescing operator
is going to prove useful in a situation where ??= would make sense.
We'll see over the next couple of years - although of course, that
depends on people knowing about it. It's one of the least advertised
new features of C# 2.0, IMO.

Jon

Jan 23 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Baldur Norddahl | last post: by
5 posts views Thread by Brian Keating | last post: by
4 posts views Thread by EL OSO | last post: by
10 posts views Thread by Wilhelm Heramb | last post: by
11 posts views Thread by Jon Shemitz | last post: by
4 posts views Thread by shapper | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.