Skip to content

Instantly share code, notes, and snippets.

@mpetrinidev
Created September 19, 2019 16:26

Revisions

  1. mpetrinidev created this gist Sep 19, 2019.
    25 changes: 25 additions & 0 deletions Program.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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();
    }
    }