In C#, ?? operatore è noto come operatore di coalescenza nulla. Restituirà il valore del suo operando di sinistra se non è null. Se è nullo, valuterà l'operando di destra e restituirà il risultato. Oppure, se l'operando di sinistra restituisce un valore diverso da null, non valuta l'operando di destra.
Sintassi:
p ?? q>
Qui p è l'operando sinistro e q è l'operando destro di ?? operatore. Il valore di p può essere di tipo nullable, ma il valore di q deve essere di tipo non nullable. Se il valore di p è nullo, restituisce il valore di q. Altrimenti, restituirà il valore di p.
Punti importanti:
- IL ?? L'operatore viene utilizzato per verificare i valori null ed è anche possibile assegnare un valore predefinito a una variabile il cui valore è null (o tipo nullable).
- Non ti è permesso sovraccaricare ?? operatore.
- È associativo destro.
- In ?? operatore, puoi utilizzare l'espressione Throw come operando di destra di ?? operatore che rende il codice più conciso.
- Ti è permesso usare?? operatore con tipi di valore e tipi di riferimento.
Esempio:
classe di stringhe Java
// 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;>>}>}>}>>
>
Produzione:
Value of item_4 is: techcodeview.com Value of item_3 is: techcodeview.com Value of item_6 is: 40>
- Con l'aiuto di ?? operatore che puoi impedire InvalidOperationException .
Esempio:
.net tutorial
// 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);>>}>}>}>>
>
Produzione:
Value of item_1 is: Value of item_2 is: 100>
- Con l'aiuto di ?? operatore puoi rimuovere molte condizioni if-else ridondanti e rendere il tuo codice compatto e leggibile.
Esempio:
istanza Java di
// 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);>>}>}>}>>
>
Produzione:
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);>>}>}>}>confronto tra leone e tigre
>
>
modello tcp e ip
Produzione:
Value of item_1 is: Value of item_2 is: 200>
- ?? l'operatore può essere annidato. Renderà il tuo codice più leggibile e ridurrà anche le molteplici condizioni if-else.
Esempio:
// 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);>>}>}>}>>
>
Produzione:
Value of item_4 is: 500>