?? Null Coalescing Operator --> What does coalescing mean?

C#TerminologyCoalesceNull Coalescing-Operator

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

> 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.

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

Meaning take the first non-null value.

Solution 5 - C#

You can start from this youtube video http://www.youtube.com/watch?v=YJGGmTNHPeo

If you see the English meaning of coalescing it says “consolidate together”. Coalescing operator returns the first NON-NULL value from a chain.

For example below is a simple coalescing code which chains four strings.So if “str1” is null it will try “str2” , if “str2” is null it will try “str3” and so on until it finds a string with a non-null value.

string final =str1 ??  str2 ?? str3 ??

Solution 6 - C#

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.

Solution 7 - C#

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.

Solution 8 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRob P.View Question on Stackoverflow
Solution 1 - C#John FeminellaView Answer on Stackoverflow
Solution 2 - C#Mark CidadeView Answer on Stackoverflow
Solution 3 - C#Erich MirabalView Answer on Stackoverflow
Solution 4 - C#p.campbellView Answer on Stackoverflow
Solution 5 - C#Shivprasad KoiralaView Answer on Stackoverflow
Solution 6 - C#Robin DayView Answer on Stackoverflow
Solution 7 - C#Ciro CorvinoView Answer on Stackoverflow
Solution 8 - C#tbaskanView Answer on Stackoverflow