Core C#

Null Coalescing Operators (??)

This C# program demonstrates how use the null coalescing operator.

Program.cs

using System;

namespace XoaX {
    class Program {
        static void Main(string[] args) {
            string[] saPersons = new string[] { "Father", "Son", "Holy Spirit", "" };
            string[] saCheck = new string[] { null, null, null, null };
            for (int i = 0; i < 4; ++i) {
                // The null coalescing chain selects the first that is not null
                Console.WriteLine(saCheck[2] ?? saCheck[1] ?? saCheck[0] ?? "All null");
                saCheck[i] = saPersons[i];
            }
        }
    }
}
 

Output

All null
Father
Son
Holy Spirit
Press any key to continue . . .
 
 

© 2007–2025 XoaX.net LLC. All rights reserved.