Using Null Coalescing Operator in C#


Grammarly

More than 40 Lakhs Students and Professionals use Grammarly for their spell and grammar checks. You can register and try for free.

Sometimes , you may want to simply the Null checks in your .NET Program without much usage of the if statements .

The Null Coalescing Operator can be used in this scenario.

Using Null Coalescing Operator in C#

using System;
namespace AbundantCode
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Employee emp = new Employee { Name = "Mark", ID = 1 };
            var returnvalue = emp ?? new Employee();

            Console.WriteLine(returnvalue.Name);
            Console.ReadLine();
        }
    }
}

Grammarly

More than 40 Lakhs Students and Professionals use Grammarly for their spell and grammar checks. You can register and try for free.
%d bloggers like this: