Null-Coalescing Operator - CSharp Language Basics

CSharp examples for Language Basics:Null Operator

Introduction

The ?? operator is the null-coalescing operator.

If the operand is non-null, get its value; otherwise, get its default value.

For example:

Demo Code

using System;/*from   ww w  .j a va2  s.  c  om*/
class Test
{
   static void Main(){
      string s1 = null;
      string s2 = s1 ?? "nothing";   // s2 evaluates to "nothing"
      Console.WriteLine (s1);
      Console.WriteLine (s2);
   }
}

Result


Related Tutorials