Conditional ? Operator In C#



TODO:

Have you ever wanted to express an if-else more consicely and elegantly?  The example below checks for a null string and sets to empty if found to be so.

 

SOLUTION: 

string mystring = "";
..
//some code
..
mystring = (mystring == null ? "" : mystring);

 

NOTES:

No notes apply to this topic.



Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading