.Net Interview Questions and Answers

Search:

C# and .NET interview questions on Coalescing operator.

By : Shiv Prasad Koirala | Dec 16th, 2013 | Views : 3229 | Comments : 0 Bookmark and Share

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.


string final =str1 ??  str2 ?? str3 ?? str4;


What is the benefit of coalescing?

You do not need to write long if condition as shown below.


if (str1 != null)

                final = str1;

            }

            else

           

                if (str2 != null)

               

                    final = str2;

                }

                else

               

                    if (str3 != null)

                   

                        final = str3;

                    }

                    else

                   

                        if (str4 != null)

                       

                            final = str4;

                        }

                    }

                }

            }


Below is a nice video created www.questpond.com on Coalescing operator in c#


 
Rate this Article :
1 Star
2 Stars
3 Stars
4 Stars
5 Stars
1 1 1 2 1
 
 
 
 

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

Article Categories

Learn in Hindi