📜  C#中的空合并运算符

📅  最后修改于: 2021-05-29 17:43:55             🧑  作者: Mango

在C#中, ??运算符称为Null-coalescing运算符。如果不为空,它将返回其左操作数的值。如果为null,则它将评估右侧操作数并返回其结果。或者,如果左侧操作数的计算结果为非null,则它不评估其右侧操作数。

句法:

p ?? q

在这里,p是??的左操作数,而q是??的右操作数。运算符。 p的值可以是可为null的类型,但是q的值必须为不可为null的类型。如果p的值为null,则返回q的值。否则,它将返回p的值。

重要事项:

  • 这 ??运算符用于检查空值,还可以将默认值分配给值为空(或可为空的类型)的变量。
  • 您不允许超载?运算符。
  • 它是右关联的。
  • 在 ??运算符,可以将throw表达式用作??的右侧操作数。运算符,使您的代码更简洁。
  • 您被允许使用??值类型和引用类型的运算符。

    例子:

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

    输出:

    Value of item_4 is: GeeksforGeeks 
    Value of item_3 is: GeeksforGeeks
    Value of item_6 is: 40
    
  • 在…的帮助下 ??运算符,可以防止InvalidOperationException

    例子:

    // C# program to illustrate how ?? 
    // operator prevent the 
    // InvalidOperationException
    using System;
       
    namespace example {
       
    class GFG {
          
        // Main Method
        static void Main(string[] args)
        {
            // Creating items of nullable types
            int ? item_1 = null;
       
             /*
             Here if you use this commented part,
             then this statement will give you an
             InvalidOperationException. So to 
             overcome this problem we use ?? operator 
             int? item_2 = item_1.Value;
             */
       
            // With the help of ?? operator we 
            // assign a default value to the item_2
            // And the value of item_1 is null.
            int ? item_2 = item_1 ?? 100;
            Console.WriteLine("Value of item_1 is: {0}", item_1);
            Console.WriteLine("Value of item_2 is: {0}", item_2);
        }
    }
    }
    

    输出:

    Value of item_1 is: 
    Value of item_2 is: 100
    
  • 在…的帮助下 ??运算符,您可以删除许多多余的“ if-else”条件,并使代码紧凑且易读。

    例子:

    // C# program to illustrate how ?? 
    // operator replaces if-else statements
    using System;
       
    namespace example {
       
    class GFG {
          
        // Main Method
        static void Main(string[] args)
        {
            // Creating items of nullable types
            int ? item_1 = null;
       
            int ? item_2;
       
            if (item_1.HasValue) {
                item_2 = item_1;
            }
            else {
                item_2 = 200;
            }
            Console.WriteLine("Value of item_1 is: {0}", item_1);
            Console.WriteLine("Value of item_2 is: {0}", item_2);
        }
    }
    }
    

    输出:

    Value of item_1 is: 
    Value of item_2 is: 200
    
    // C# program to illustrate how ??
    // operator replaces if-else statements
    using System;
       
    namespace example {
       
    class GFG {
          
        // Main Method
        static void Main(string[] args)
        {
            // Creating items of nullable types
            int ? item_1 = null;
       
            // Using ?? operator
            int ? item_2 = item_1 ?? 200;
            Console.WriteLine("Value of item_1 is: {0}", item_1);
            Console.WriteLine("Value of item_2 is: {0}", item_2);
        }
    }
    }
    

    输出:

    Value of item_1 is: 
    Value of item_2 is: 200
    
  • ??运算符可以嵌套。这将使您的代码更具可读性,并减少多个if-else条件。

    例子:

    // C# program to illustrate how 
    // we use nested ?? operator
    using System;
       
    namespace example {
       
    class GFG {
          
        // Main Method
        static void Main(string[] args)
        {
            // Creating items of nullable types
            int ? item_1 = null;
            int ? item_2 = null;
            int ? item_3 = 500;
       
            // Nested ?? operator
            int ? item_4 = item_1 ?? item_2 ?? item_3;
              
            Console.WriteLine("Value of item_4 is: {0} ", item_4);
        }
    }
    }
    

    输出:

    Value of item_4 is: 500