Global Trend Radar
Web: learn.microsoft.com US web_search 2026-05-07 09:23

?? および ??= 演算子 - null合体演算子 - C# リファレンス

原題: ?? and ??= operators - null-coalescing operators - C# reference

元記事を開く →

分析結果

カテゴリ
AI
重要度
54
トレンドスコア
18
要約
C#における??および??=演算子は、null合体演算子として知られ、null値の処理を簡素化します。??演算子は、左側のオペランドがnullである場合に右側のオペランドを返し、そうでない場合は左側を返します。??=演算子は、左側の変数がnullの場合に右側の値を代入します。これにより、コードの可読性と効率が向上します。
キーワード
?? and ??= operators - null-coalescing operators - C# reference | Microsoft Learn Table of contents Exit editor mode Ask Learn Ask Learn Reading mode Table of contents Read in English Add Add to plan Edit Copy Markdown Print Note Access to this page requires authorization. You can try signing in or changing directories . Access to this page requires authorization. You can try changing directories . ?? and ??= operators - the null-coalescing operators Feedback Summarize this article for me The null-coalescing operator ?? returns the value of its left-hand operand if it's not null . Otherwise, it evaluates the right-hand operand and returns its result. The ?? operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null. The null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null . The ??= operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null. List<int>? numbers = null; int? a = null; Console.WriteLine((numbers is null)); // expected: true // if numbers is null, initialize it. Then, add 5 to numbers (numbers ??= new List<int>()).Add(5); Console.WriteLine(string.Join(" ", numbers)); // output: 5 Console.WriteLine((numbers is null)); // expected: false Console.WriteLine((a is null)); // expected: true Console.WriteLine((a ?? 3)); // expected: 3 since a is still null // if a is null then assign 0 to a and add a to the list numbers.Add(a ??= 0); Console.WriteLine((a is null)); // expected: false Console.WriteLine(string.Join(" ", numbers)); // output: 5 0 Console.WriteLine(a); // output: 0 The left-hand operand of the ??= operator must be a variable, a property , or an indexer element. The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release. The documentation identifies any feature first introduced in the last three versions of the language or in current public previews. Tip To find when a feature was first introduced in C#, consult the article on the C# language version history . The type of the left-hand operand of the ?? and ??= operators can't be a non-nullable value type. In particular, you can use the null-coalescing operators with unconstrained type parameters: private static void Display<T>(T a, T backup) { Console.WriteLine(a ?? backup); } The null-coalescing operators are right-associative. That is, expressions of the form a ?? b ?? c d ??= e ??= f are evaluated as a ?? (b ?? c) d ??= (e ??= f) Examples The ?? and ??= operators are useful in the following scenarios: In expressions that use the null-conditional operators ?. and ?[] , use the ?? operator to provide an alternative expression to evaluate if the result of the expression with null-conditional operations is null : double SumNumbers(List<double[]> setsOfNumbers, int indexOfSetToSum) { return setsOfNumbers?[indexOfSetToSum]?.Sum() ?? double.NaN; } var sum = SumNumbers(null, 0); Console.WriteLine(sum); // output: NaN When you work with nullable value types and need to provide a value of an underlying value type, use the ?? operator to specify the value to provide if a nullable type value is null : int? a = null; int b = a ?? -1; Console.WriteLine(b); // output: -1 Use the Nullable<T>.GetValueOrDefault() method if the value to use when a nullable type value is null should be the default value of the underlying value type. To make the argument-checking code more concise, use a throw expression as the right-hand operand of the ?? operator: public string Name { get => name; set => name = value ?? throw new ArgumentNullException(nameof(value), "Name cannot be null"); } The preceding example also demonstrates how to use expression-bodied members to define a property. Use the ??= operator to replace code of the following form: if (variable is null) { variable = expression; } Use the following code: variable ??= expression; Operator overloadability You can't overload the ?? and ??= operators. C# language specification For more information about the ?? operator, see The null coalescing operator section of the C# language specification . For more information about the ??= operator, see the Compound assignment section of the C# language specification. See also Null check can be simplified (IDE0029, IDE0030, and IDE0270) C# operators and expressions ?. and ?[] operators ?: operator Collaborate with us on GitHub The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide . .NET Open a documentation issue Provide product feedback Feedback Was this page helpful? Yes No No Need help with this topic? Want to try using Ask Learn to clarify or guide you through this topic? Ask Learn Ask Learn Suggest a fix? Additional resources Last updated on 2026-01-24 Was this page helpful? Yes No No Need help with this topic? Want to try using Ask Learn to clarify or guide you through this topic? Ask Learn Ask Learn Suggest a fix?

類似記事(ベクトル近傍)