Created
November 29, 2017 10:42
-
-
Save luisdeol/7208d78573e5e16bd328231acc20aacb to your computer and use it in GitHub Desktop.
Using the Conditional and Null-coalescing operator
This file contains hidden or 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
| 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