C#では、 ??オペレーター は Null 合体演算子として知られています。左側のオペランドが null でない場合は、その値を返します。 null の場合は、右側のオペランドを評価し、その結果を返します。または、左側のオペランドが非 null と評価された場合、右側のオペランドは評価されません。
構文:
p ?? q>
ここで、p は ?? の左側のオペランド、q は右側のオペランドです。オペレーター。 p の値は null 許容型にすることができますが、q の値は null 非許容型である必要があります。 p の値が null の場合は、q の値を返します。それ以外の場合は、p の値を返します。
重要な点:
- ??演算子は null 値をチェックするために使用され、値が null (または null 許容型) の変数にデフォルト値を割り当てることもできます。
- 過負荷は禁止です??オペレーター。
- 右連想です。
- で ??演算子を使用すると、 ?? の右側のオペランドとして throw 式を使用できます。コードをより簡潔にする演算子。
- ??の使用を許可されています。値型と参照型を持つ演算子。
例:
slf4j と log4j
// 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;>>}>}>}>>
>
出力:
Value of item_4 is: techcodeview.com Value of item_3 is: techcodeview.com Value of item_6 is: 40>
- ??の助けを借りて阻止できるオペレーター 無効な操作例外 。
例:
// 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 条件を削除し、コードをコンパクトで読みやすくすることができます。
例:
powershell 以下
// 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>
Javaの数学
// 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 条件も減ります。
例:
Java文字列のトリム
// 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>