C# ternary operator (?:) and Null Coalescing Operator (??)

Using if statement

if (value != null){    litCurrentSurveyName.Text = value;}else{    litCurrentSurveyName.Text = string.Empty;}

Using ternary operator (?:)

litCurrentSurveyName.Text = value != null ? value : string.Empty;

null-coalescing operator (??) which is a shortcut for the ternary operator (?:)

litCurrentSurveyName.Text = value ?? string.Empty;

Leave a Reply

Your email address will not be published. Required fields are marked *