Possible Duplicate:
What is the “??” operator for?
I saw a line of code which states –
return (str ?? string.Empty).Replace(txtFind.Text, txtReplace.Text);
I want to know the exact meaning of this line(i.e. the ?? part)..
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s the null coalescing operator: it returns the first argument if it’s non null, and the second argument otherwise. In your example,
str ?? string.Emptyis essentially being used to swap null strings for empty strings.It’s particularly useful with nullable types, as it allows a default value to be specified:
Edit:
str ?? string.Emptycan be rewritten in terms of the conditional operator asstr != null ? str : string.Empty. Without the conditional operator, you’d have to use a more verbose if statement, e.g.: