Sunday, April 5, 2015

C# Null Coalescing Operator

Kindly find the example for Null Coalescing Operator.
    
 //Without Null Coalescing Operator
            int AvailableTickets;
            int? TicketsOnSale = 89;

            if (TicketsOnSale == null)
            {
                AvailableTickets = 0;
            }
            else
            {
                AvailableTickets = (int)TicketsOnSale;
            }

            Console.WriteLine("Available Tickets={0}",AvailableTickets);

            //Console.WriteLine("Available Tickets= " + AvailableTickets);
            Console.Read();

            //With Null Coalescing Operator

            int AvailableTickets;
            int? TicketsOnSale = 78;

            AvailableTickets = TicketsOnSale ?? 45;

            Console.WriteLine("Available Tickets={0}", AvailableTickets);

            Console.Read();

No comments:

Post a Comment