C# language feature:

  • Null propagator (null-conditional operator, succinct null checking)

applies to

  • objects

benefit

  • Simplifies checking for null references.

how to use

  • Simply replace the member access . with ?. Use this construct with the null coalescing operator ?? to assign default values when one of the properties are null.

example:

        class Null_Propagator_Example
        {
            void Use_of_Null_Propagor(string somestring)
            {
                int length = somestring?.Length ?? -1;
                System.Console.WriteLine($"somestring has {length} characters");  // lenght is -1 if somestring is null.
            }    
        }