Would it be possible to add a new operator to the String class that looked something like
string val = anotherVal ??? "Default Val";
and worked like
string val = !String.IsNullOrEmpty(anotherVal) ? anotherVal : "Default Val";
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.
You can’t define your own operators for the string class and use them from C#. (F# allows you to create operators for arbitrary classes, a bit like extension methods but for other member types.)
What you can do is write an extension method:
And call it with:
If you don’t want to evaluate the “default” string unless it’s needed, you could have an overload taking a function:
And call it with: