Null-Coalescing operátor v C#

V C#, ?? operátor je známy ako operátor Null-Coalescing. Ak nie je nulový, vráti hodnotu svojho ľavého operandu. Ak je null, potom vyhodnotí pravý operand a vráti svoj výsledok. Alebo ak sa ľavý operand vyhodnotí ako nenulový, potom nevyhodnotí svoj pravý operand.

Syntax:

p ?? q 

Tu je p ľavý a q pravý operand ?? operátor. Hodnota p môže byť typu s nulovou hodnotou, ale hodnota q musí byť typu bez možnosti null. Ak je hodnota p nulová, vráti hodnotu q. V opačnom prípade vráti hodnotu p.

Dôležité body:

  • ?? operátor sa používa na kontrolu hodnôt null a môžete tiež priradiť predvolenú hodnotu premennej, ktorej hodnota je null (alebo typ s možnosťou null).
  • Nemáte dovolené preťažovať sa?? operátor.
  • Je pravo asociatívna.
  • v ?? operátor, môžete použiť výraz throw ako pravý operand pre ?? operátora, ktorý robí váš kód stručnejším.
  • Máte povolené používať ?? operátor s typmi hodnôt a referenčnými typmi.

    Príklad:




    // 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 => 'techcodeview.com'> ;> > 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} '> +> > '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;> > }> }> }>

    Výkon:

     Value of item_4 is: techcodeview.com Value of item_3 is: techcodeview.com Value of item_6 is: 40 
  • S pomocou ?? operátora, ktorému môžete zabrániť InvalidOperationException .

    Príklad:




    // 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);> > }> }> }>

    Výkon:

     Value of item_1 is: Value of item_2 is: 100 
  • S pomocou ?? pomocou operátora môžete odstrániť veľa nadbytočných podmienok, ak je to inak, a urobiť váš kód kompaktným a čitateľným.

    Príklad:




    // 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);> > }> }> }>

    Výkon:

     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);> > }> }> }>

    Výkon:

     Value of item_1 is: Value of item_2 is: 200 
  • ?? operátor môže byť vnorený. Vďaka tomu bude váš kód čitateľnejší a tiež sa zníži počet stavov if-else.

    Príklad:




    // 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);> > }> }> }>

    Výkon:

    Value of item_4 is: 500