Created
September 19, 2019 16:26
-
-
Save mpetrinidev/0864c22bb94df75331d6b6035a8a1106 to your computer and use it in GitHub Desktop.
youtube-nullcoalescing-c#8
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
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
static int? GetNullAge() => null; | |
static int? GetAge() => 27; | |
int? age = GetNullAge(); | |
//Old Way | |
//if (age == null) | |
//{ | |
// age = GetAge(); | |
//} | |
//C# 7 | |
//age = age ?? GetAge(); | |
//C# 8.0 | |
age ??= GetAge(); | |
Console.WriteLine(age); | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment