Skip to content

Instantly share code, notes, and snippets.

@luisdeol
Created November 29, 2017 10:42
Show Gist options
  • Save luisdeol/7208d78573e5e16bd328231acc20aacb to your computer and use it in GitHub Desktop.
Save luisdeol/7208d78573e5e16bd328231acc20aacb to your computer and use it in GitHub Desktop.
Using the Conditional and Null-coalescing operator
namespace implement_program_flow
{
class Program
{
static void Main(string[] args)
{
// Null-coalescing operator
var words = new[] { "Luis", "Felipe", "Jorge", "Joel", "Helora" };
var richardName = words.SingleOrDefault(w => w == "Richard") ?? "Default"; // FirstOrDefault returns null if there is no "Richard"
Console.WriteLine(richardName);
// The conditional operator
var hoursSinceLastMeal = 5;
var luisMood = hoursSinceLastMeal > 3 ? "Luis is not really happy..." : "Luis is still cool!";
Console.WriteLine(luisMood);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment