473,388 Members | 923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 1986
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Baldur Norddahl | last post by:
Hi, How come "X=null" is not the same as "X is null"? I got a few selects with queries like this: select * from foo where customer=#customer# or (#customer# is null and customer is null) ...
5
by: Brian Keating | last post by:
Hi there, Just wondering what is the correct way of implementing the comparison operator in c# operator== My problem is when one or more of the objects is null, I've cast to object to try...
4
by: EL OSO | last post by:
Hi, let's suppose I have a class A { int x; ... public static bool operator == (A a1 , A a2) { return (a1.x==a2.x); }
10
by: Wilhelm Heramb | last post by:
What is the best practice to implement operator overloading for == and != that handles null on either lhs or rhs. Andreas :-)
11
by: Jon Shemitz | last post by:
The new ?? operator doesn't seem to be in the precedence table in VS.05 <ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_csref/html/214e7b83-1a41-4f7c-9867-64e9c0bab39f.htm>, and I...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
12
by: Zeba | last post by:
Hi .... I'm always getting stuck with null reference exceptions ! Can you tell me what's wrong with my code given below ? I'm getting the error that reference is not set to an instance of an...
4
by: shapper | last post by:
Hello, I have the following: DateTime c = database.Posts.Max(b =b.CreatedAt).Value; b.CreatedAt is of type "DateTime?" The problem is when database.Posts is empty. I get an error:...
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.