Cª#, ?? operador se conoce como operador coalescente nulo. Devolverá el valor de su operando de la izquierda si no es nulo. Si es nulo, evaluará el operando de la derecha y devolverá su resultado. O si el operando de la izquierda se evalúa como no nulo, entonces no evalúa su operando de la derecha.
Sintaxis:
p ?? q>
Aquí, p es el operando izquierdo y q es el operando derecho de ?? operador. El valor de p puede ser de tipo anulable, pero el valor de q debe ser de tipo no anulable. Si el valor de p es nulo, devuelve el valor de q. De lo contrario, devolverá el valor de p.
Puntos importantes:
- El ?? El operador se utiliza para verificar valores nulos y también puede asignar un valor predeterminado a una variable cuyo valor es nulo (o tipo anulable).
- ¿No puedes sobrecargar? operador.
- Es asociativo por derecha.
- En ?? operador, puede utilizar la expresión throw como operando del lado derecho de ?? operador que hace que su código sea más conciso.
- ¿Tienes permiso para usar? operador con tipos de valor y tipos de referencia.
Ejemplo:
// 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;>>}>}>}>>
>
Producción:
Value of item_4 is: techcodeview.com Value of item_3 is: techcodeview.com Value of item_6 is: 40>
- Con la ayuda de ?? operador usted puede prevenir Excepción de operación no válida .
Ejemplo:
topología de las estrellas
// 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);>>}>}>}>>
>
Producción:
Value of item_1 is: Value of item_2 is: 100>
- Con la ayuda de ?? operador puede eliminar muchas condiciones if-else redundantes y hacer que su código sea compacto y legible.
Ejemplo:
// 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);>>}>}>}>>
herramienta de curación gimp
>
Producción:
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);>>}>}>}>cómo leer desde un archivo csv en java
>
>
Producción:
Value of item_1 is: Value of item_2 is: 200>
- ?? El operador se puede anidar. Hará que su código sea más legible y también reducirá múltiples condiciones if-else.
Ejemplo:
// 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);>>}>}>}>>
>
Producción:
Value of item_4 is: 500>