C# and .NET interview questions on Coalescing operator.
When should we use "??" (NULL Coalescing operator) ?
"??" is a NULL coalescing operator. 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.
What is the benefit of coalescing?
You do not need to write long if condition as shown below.
Below is a nice video created www.questpond.com on Coalescing operator in c#