Skip to content

Instantly share code, notes, and snippets.

@Phuseos
Created June 22, 2016 07:43
Show Gist options
  • Select an option

  • Save Phuseos/dee135aeed21919a0d56215171c44cb1 to your computer and use it in GitHub Desktop.

Select an option

Save Phuseos/dee135aeed21919a0d56215171c44cb1 to your computer and use it in GitHub Desktop.
Null-conditional operators example (C#)
/**Null-conditional operators example**/
//If the Text in txtBox1 is null, return 0, else return the length of the string of txtBox1.
int RunMe;
RunMe = txtBox1?.Text.Length ?? 0;
//The above statement is the equivalent of the if statement below
if (txtBox1.Text.Length != 0) RunMe = txtBox1.Text.Length;
else RunMe = 0;
//If the TextBox has the value ('Me@Me.com') then RunMe will have a value of 9. If it's empty, RunMe will be too.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment