Null-coalescing and Ternary operator

There is a smarter way of avoiding null values in string assignment. Many of us use Ternary, Format etc. to handle this.
string studentHobbies = (txtHobbies.Text != null) ? txtHobbies.Text : string.Empty;

can be written smartly using null-coalescing operator (??) as-
string studentHobbies = txtHobbies.Text ?? string.Empty;


Happy Coding :)
Share on Google Plus

About Unknown

0 comments :

Post a Comment