logo

Null-comalescing operators C#

C# valodā, ?? operators ir pazīstams kā Null-coalescing operators. Tas atgriezīs sava kreisās puses operanda vērtību, ja tā nav nulles vērtība. Ja tas ir nulle, tas novērtēs labās puses operandu un atgriež rezultātu. Vai arī, ja kreisās puses operands novērtē līdz nullei, tad tas nenovērtē savu labās puses operandu.

Sintakse:

p ?? q>

Šeit p ir kreisais un q ir labais operands ?? operators. P vērtība var būt nullable type, bet q vērtībai ir jābūt non-nulleable type. Ja p vērtība ir nulle, tad tā atgriež q vērtību. Pretējā gadījumā tas atgriezīs p vērtību.



Svarīgi punkti:

  • ?? operators tiek izmantots, lai pārbaudītu nulles vērtības, un jūs varat arī piešķirt noklusējuma vērtību mainīgajam, kura vērtība ir null (vai nullable type).
  • Jums nav atļauts pārslogot?? operators.
  • Tas ir labēji-asociatīvs.
  • iekšā ?? operatoru, jūs varat izmantot metiena izteiksmi kā labās puses operandu ?? operatoru, kas padara jūsu kodu kodolīgāku.
  • Jums ir atļauts izmantot ?? operators ar vērtību veidiem un atsauces veidiem.

    Piemērs:




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

    >

    >

    Izvade:

     Value of item_4 is: techcodeview.com Value of item_3 is: techcodeview.com Value of item_6 is: 40>
  • Ar ?? operatoru, kuru varat novērst InvalidOperationException .

    Piemērs:


    java metiena izņēmums



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

    kā atrast slēptās lietotnes operētājsistēmā Android

    >

    >

    Izvade:

     Value of item_1 is: Value of item_2 is: 100>
  • Ar ?? operatoru, varat noņemt daudzus liekus ja-cits nosacījumus un padarīt savu kodu kompaktu un lasāmu.

    Piemērs:




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

    >

    >

    Izvade:

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

    >

    >

    galvenie java intervijas jautājumi

    Izvade:

     Value of item_1 is: Value of item_2 is: 200>
  • ?? operatoru var ligzdot. Tas padarīs jūsu kodu lasāmāku, kā arī samazinās vairākus ja-cits nosacījumus.

    Piemērs:




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

    >

    >

    Izvade:

    Value of item_4 is: 500>