Skip to content

Instantly share code, notes, and snippets.

@zs40x
Last active April 9, 2016 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zs40x/7eabaef8985559290cbf4ffc6b9fe37d to your computer and use it in GitHub Desktop.
Save zs40x/7eabaef8985559290cbf4ffc6b9fe37d to your computer and use it in GitHub Desktop.
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