📜  C#8.0中的Null-Coalescing赋值运算符

📅  最后修改于: 2021-05-29 14:15:33             🧑  作者: Mango

C#8.0引入了一个新的运算符,称为Null-coalescing赋值运算符( ?? = )。仅当左操作数的值为null时,才使用此运算符将其右操作数的值分配给其左操作数。如果左侧操作数的计算结果为非null,则此运算符不会评估其右侧操作数。

句法:

p ??= q

这里,p是?? =运算符的左操作数,q是右操作数。如果p的值为空,则?? =运算符将p中的q的值赋值。或者,如果p的值不为null,则它不求q。

重要事项:

  • ?? =运算符的左侧操作数必须是变量,属性或索引器元素。
  • 它是右关联的。
  • 您不能重载?? =运算符。
  • 您可以将?? =运算符与引用类型和值类型一起使用。

    例子:

    // C# program to illustrate how to use 
    // ??= operator with value types and 
    // reference types
    using System;
       
    namespace example {
       
    class GFG {
       
        static void Main(string[] args)
        {
       
            // Reference types
            string item_1 = null;
            string item_2 = null;
            string item_4 = "GeeksforGeeks";
            string item_5 = "GFG";
       
            // Using ??= operator
            item_1 = item_1 ??= item_4;
            string item_6 = item_2 ??= item_5;
       
            Console.WriteLine("Value of item_1 is: {0}\n"+
               "Value of item_6 is:{1}", item_1, item_6);
       
            // Value types
            int ? ele_1 = null;
       
            GFG obj = new GFG();
       
            // Using ??= operator assigns
            // the value of ele_1
            // And also you are allowed to 
            // use method with ??= operator
            ele_1 = ele_1 ??= obj.Add(10, 30);
            Console.WriteLine("Value of ele_1 is {0}", ele_1);
        }
       
        // Method
        public int Add(int a, int b)
        {
            int result = a + b;
            return result;
        }
    }
    }
    

    输出:

    Value of item_1 is: GeeksforGeeks
    Value of item_6 is:GFG
    Value of ele_1 is 40
    
  • 借助?? =运算符,您可以删除许多多余的“ if-else”条件,并使您的代码更紧凑,更易读。如下例所示:

    例子:

    // C# program to illustrate how to use
    // ??= operator to remove if statements
    using System;
       
    namespace example {
       
    class GFG {
          
        // Main Method
        static void Main(string[] args)
        {
            // Nullable variable
            int ? element = null;
       
            // Checking the element is null or not
            if (element is null) {
                  
                // Assign a new value to the element
                // Using ??= operator
                int ? new_element = element ??= 400;
                Console.WriteLine("Element is null. "+
                 "So the new element is: {0}", new_element);
            }
        }
    }
    }
    

    输出:

    Element is null. So the new element is: 400
    
    // C# program to illustrate how to use
    // ??= operator to remove if statements
    using System;
       
    namespace example {
       
    class Program {
        static void Main(string[] args)
        {
            // Nullable variable
            int ? element = null;
       
            // Using ??= operator
            // Assign values to the null variable
            element ??= 400;
            Console.WriteLine("Element is: {0}", element);
        }
    }
    }
    

    输出:

    Element is: 400
  • 您还可以像嵌套链一样使用?? =运算符。它使您的代码更具可读性。

    例子:

    // C# program to illustrate 
    // how to nest ??= operator
    using System;
       
    namespace example {
       
    class GFG {
          
        // Main Method
        static void Main(string[] args)
        {
            // Nullable variables
            int ? ele1 = null;
            int ? ele2 = null;
            int ? ele3 = 45;
       
            // Using Nested ??= operator
            int ? result = ele1 ??= ele2 ??= ele3;
              
            Console.WriteLine("Element is: {0}", result);
        }
    }
    }
    

    输出:

    Element is: 45