?? Null Coalescing Operator --> What does coalescing mean?
Asked Answered
L

7

64

I'm tempted to lie and say that English is my second language, but the truth is that I just have no idea what 'Coalescing' means. I know what ?? 'does' in C#, but the name doesn't make sense to me.

I looked up the word and I understand it to be a synonym for 'join'. 'Null Join Operator' still doesn't make sense.

Can someone enlighten me?

Lyontine answered 20/4, 2009 at 21:31 Comment(1)
Look at it from a SQL perspective!Dandiprat
L
45

I'm tempted to lie and say that English is my second language...but the truth is that I just have no idea what 'Coalescing' means. I know what ?? 'does' in C#, but the name doesn't make sense to me.

I looked up the word and I understand it to be a synonym for 'join'.

I'd say a more accurate description of "coalesce" would be "to form one thing from different elements". The "coalescing" of the ?? operator happens because a single value is always resolved from one of the two values. The first non-null value is the result.

Louella answered 20/4, 2009 at 21:35 Comment(1)
Let's be honest, one would not imagine from the various dictionary definitions ("unite into a whole", "arise from the combination of distinct elements") that coalescing would be picking one element and discarding all traits of the others, especially if several of them have significance (value). IMHO it looks like a misnomer that has been widely accepted by convention for lack of an alternative concise word.Dunlop
R
22

Coalescing is when you have more than one item and then you end up with exactly one—either by joining the items together or by choosing a single item from the group. In the case of the ?? operator, you're choosing the first non-null value of the pair of values.

Reinaldo answered 20/4, 2009 at 21:37 Comment(0)
S
9

Here are some other definitions of coalesce that might help make sense. From Answers, it shows that it means to "grow together; fuse" or "to come together so as to form one whole." In other words, take a sequence of items and make one out of them. So considering that null in this discussion means "empty," coalescing null with a non-empty gives you the non-empty.

Scatology answered 20/4, 2009 at 21:38 Comment(0)
L
6

Meaning take the first non-null value.

Labe answered 20/4, 2009 at 21:33 Comment(0)
M
2

http://www.merriam-webster.com/dictionary/coalesce

I think the best definition is the "unite for a common end". So basically pulling it all together to get the best. In programming terms it's more getting the first best item.

Mechanism answered 20/4, 2009 at 21:39 Comment(0)
J
2

This is a very old question but I wanted to add an answer.

And I think C# language designers took the word from T-SQL function COALESCE.

A comment to the question "The meaning of coalesce in SQL" on English Stack Exchange explains the meaning of function in SQL beautifully in my opinion:

The SQL function is really a shorthand name for null-coalescence. I'd say this is exactly right: null-coalescence is a subtype of the highlighted meaning here. The difference is only that the two elements used to form the resulting whole are not combined as such, but compared and selected from: if the first element is NULL (i.e., pure nothingness), it is ignored; otherwise, the second element is ignored. So essentially it ‘coalesces’ (subsumes) null into the other element. It is a bit of a stretch of the original meaning, but definitely does seem to be a development based on it.

Jezabelle answered 4/10, 2021 at 0:45 Comment(2)
SQL's COALESCE completely ignores the values after the first non-null one, and the null-coalescing operator ignores the right operand if the left operand is non-null. It's more than a bit of a stretch IMHO, it's a misnomer that people have simply accepted for lack of a better concise word.Dunlop
Yes there is a bit difference. SQL's COALESCE takes variable number of arguments whereas C# ?? operator is a binary operators, there is left hand and right hand. But you can chain the operator like var1 ?? var2 ?? var3?? Method1() ?? var4. With chaining I think they more or less look and behave the same way: return the first non-null value and ignore the rest. Since English is not my first language I can't comment if 'coalesce' a good fit but you seem to be right since dictionaries explain it as 'come together to form one mass or whole' which doesn't help to understand the operators :-)Jezabelle
S
0

Coalescing word derives from Latin language, and means "to join together" something. In particular it designates in physical chemistry, a phenomenon in which small drops of a liquid dispersed in another immiscible liquid tend to join the larger ones, forming larger aggregates; this is called "Coalescence".

In C# context, for extension, this "join" happens between variables thanks to the null coalescing operator, but the resulting value depends from the fact that the first operand is null or not, if it is, then the reulting value will be that of the second operand.

Suffragette answered 20/5, 2019 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.