Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

C# Null Coalescing operator

by suresh

C# Null Coalescing operator ?? is a binary operator, and it can be used with both nullable types and reference types.

The syntax is of the C# Null Coalescing operator as follows:

a ?? b

If a is non-null value, then evaluate to a, otherwise b

Let us see an example to understand the usage of the Null Coalescing operator.

C# Null Coalescing operator Example

TicketsAvailable is a non-nullable int variable, whereas TicketsonSale is a nullable int variable holding the value of Null. In the if condition, we are checking whether TicketsonSale is null.

Here, in this case, we have assigned TicketsonSale to null, so obviously the result will be

TicketsAvailable = 0

C# code without using Null Coalescing

using System;

namespace CSharp_Tutorial
{
    class Without_NullCoalesce
    {
        static void Main()
        {
            int TicketsAvailable;
            int? TicketsonSale = null;

            if (TicketsonSale == null)
            {
                TicketsAvailable = 0;
            }
            else
            {
                TicketsAvailable = (int)TicketsonSale;
            }

            Console.WriteLine("TicketsAvailable = {0}", TicketsAvailable);
        }
    }
}

OUTPUT

C# Null Coalescing Operator 1

Now let us initialize, TicketsonSale = 100, it means the else part will be executed.  

The reason why we have typecast TicketsonSale to Non-Nullable int variable is,

TicketsonSale is a nullable int variable, and we are trying to store the value of nullable int variable into a non-nullable int variable, i.e., TicketsAvailable.

It gives an error. So it is a process of Converting the result of nullable int type to non-nullable int type and then storing in a non-nullable int variable TicketsAvailable. 

Now using Null Coalescing operator(??), we can reduce the if statement part to a single line of code, i.e., 

TicketsAvailable = TicketsonSale ?? 0;

This single line of the C# code is saying that if ‘TicketsonSale’ is null return 0. And if it is not null, then return the value in the TicketsAvailable, Which, in this case, returns 100.

C# code using Null Coalescing Operator

using System;

namespace CSharp_Tutorial
{
    class Null_Coalesing
    {
        static void Main()
        {
            int TicketsAvailable;
            int? TicketsonSale = 100;

            //Using Coalesce operator ??
            TicketsAvailable = TicketsonSale ?? 0;


            Console.WriteLine("TicketsAvailable  = {0}", TicketsAvailable);
        }
    }
}

OUTPUT

C# Null Coalescing Operator 2

TicketsAvailable = 100 because TicketsonSale = 100 i.e., not null

If TicketsonSale = Null then the TicketsAvailable would be equal to 0.

Placed Under: C#

  • Dot Net Framework
  • C# Basic Example Program
  • C# Variables
  • C# Constant
  • C# Keywords
  • C# Regular Expressions
  • C# Built in Data Types
  • C# Nullable Types
  • C# Data type Conversion
  • C# Date and Time Format
  • C# Enum or Enumerator
  • C# Value and Reference types
  • C# Operators
  • C# Arithmetic Operators
  • C# Assignment Operators
  • C# Bitwise Operators
  • C# Logical Operators
  • C# Null Coalescing operator
  • C# Relational Operators
  • C# Ternary Operator
  • C# Unary Operators
  • C# If Statement
  • C# If Else Statement
  • C# Else if Statement
  • C# Nested If Statement
  • C# Break Statement
  • C# Continue Statement
  • C# goto statement
  • C# Switch Case
  • C# While Loop
  • C# Do while loop
  • C# For Loop
  • C# Foreach loop
  • C# String Builder
  • C# String
  • C# String Functions
  • C# Array
  • C# Array Functions
  • C# Multi Dimensional Arrays
  • C# Jagged Array
  • C# OOPS Introduction
  • C# Constructor
  • C# Destructor
  • C# Access Modifiers
  • C# Inheritance
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy