I know what the coalesce operator is.

By 1 lyml on December 17, 2007

The operator is ?? you use it like this:

Object o = variable ?? new Object();

I've only seen it in the c# language though.

Embed Claim Make a related claim

Discussion (9)

http://lyml.myopenid.com/

1 lyml who agreed, says

Well, it wasn't supposed to explain it, it was more so that people who were aware of the operator but didn't know it's name could properly answer.

The function is simple though, it returns the first non-null value.

null ?? new Object()

returns the new object

new Object() ?? oldObject

returns the new object

oldObject ?? new Object()

returns oldObject if it's not null otherwise new object

I hope that made sence to people :)

Make a related claim 11 months ago (link)
http://lyml.myopenid.com/

1 lyml who agreed, says

If you're asking about the ?: operator, the conditional operator then:

the statement:
oldObject ?? new Object()
is equivalent to:
oldObject != null ? oldObject : new Object()

So the coalesce operator is a subset of the conditional operator.

Make a related claim 11 months ago (link)
http://ozamosi.flukkost.nu/

3 ozamosi who hasn't voted, says

My guess is that the or operator in C# returns either True or False. But I'm not sure...

Make a related claim 11 months ago (link)
http://lyml.myopenid.com/

1 lyml who agreed, says

Ah yes, ozamosi is correct. C# is strictly typed so the or-operator (valueA || valueB) only returns true or false.

Make a related claim 11 months ago (link)
http://vynce.myopenid.com/

8 Vynce who agreed, says

SQL also has (or at least is supposed to have) a coalesce function; it's called coalesce. Perl's "or" can be used this way, and I suspect this may be true in other languages as well.

Make a related claim 11 months ago (link)
http://wonko.com/

7 Ryan Grove who agreed, says

Vynce's suspicions are well-founded. The 'foo = bar || baz' method for getting the first non-falsy value works well in many other dynamically-typed languages, including Ruby and JavaScript. It can have unpredictable results in PHP, though, and is best avoided in that language.

Make a related claim 11 months ago (link)
http://vynce.myopenid.com/

8 Vynce who agreed, says

well, PHP is best avoided in that language.

Make a related claim 11 months ago (link)
http://logicaldog.myopenid.com/

4 Logical Dog who hasn't voted, says

And Go is better than chess, too. We know, we know, already! good grief ...

Make a related claim 10 months ago (link)
http://darch.myopenid.com/

6 D'Archangel who disagreed, says

"And Go is better than chess, too."

Victory for ZIM!

D'A
... 7k AGA

Make a related claim 10 months ago (link)
Sign in in to leave a comment.