Last active
April 9, 2016 09:05
-
-
Save zs40x/7eabaef8985559290cbf4ffc6b9fe37d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ConsoleApplication | |
{ | |
public class Program | |
{ | |
static void Main() | |
{ | |
Console.WriteLine(getWithTernaryOperator()); | |
Console.WriteLine(getWithNullCoalescingOperator()); | |
} | |
static string getWithTernaryOperator() | |
{ | |
string variableThatMayBeNull = null; | |
string defaultValue = "Default"; | |
// do something useful.. | |
return variableThatMayBeNull != null | |
? variableThatMayBeNull : defaultValue; | |
} | |
static string getWithNullCoalescingOperator() | |
{ | |
string variableThatMayBeNull = null; | |
string defaultValue = "Default"; | |
// do something useful... | |
return variableThatMayBeNull ?? defaultValue; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment