๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ csharp โ€บ language-reference โ€บ operators โ€บ null-coalescing-operator
?? and ??= operators - null-coalescing operators - C# reference | Microsoft Learn
The `??` and `??=` operators are the C# null-coalescing operators. They return the value of the left-hand operand if it isn't null. Otherwise, they return the value of the right-hand operand
binary operator that is part of the syntax for a basic conditional expression in several programming languages
The null coalescing operator is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, such as (in alphabetical order): C# since version 2.0, โ€ฆ Wikipedia
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Null_coalescing_operator
Null coalescing operator - Wikipedia
October 31, 2025 - The null coalescing operator is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, such as (in alphabetical order): C# since version 2.0, Dart since version 1.12.0, PHP since version 7.0.0, Perl since version 5.10 as logical defined-or, PowerShell since 7.0.0, and Swift as nil-coalescing operator.
Top answer
1 of 4
37

Yup:

tb_myTextBox.Text = o.Member ?? "default";

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator

2 of 4
25

Well, it's not quite the same as the conditional operator, but I think you're thinking of the null coalescing operator (??). (I guess you did say it was "similar" :) Note that "ternary" just refers to the number of operands the operator is - so while the conditional operator is a ternary operator, the null coalescing operator is a binary operator.

It broadly takes this form:

result = first ?? second;

Here second will only be evaluated if first is null. It doesn't have to be the target of an assignment - you could use it to evaluate a method argument, for example.

Note that the first operand has to be nullable - but the second doesn't. Although there are some specific details around conversions, in the simple case the type of the overall expression is the type of the second operand. Due to associativity, you can stack uses of the operator neatly too:

int? x = GetValueForX();
int? y = GetValueForY();
int z = GetValueForZ();

int result = x ?? y ?? z;

Note how x and y are nullable, but z and result aren't. Of course, z could be nullable, but then result would have to be nullable too.

Basically the operands will be evaluated in the order they appear in the code, with evaluation stopping when it finds a non-null value.

Oh, and although the above is shown in terms of value types, it works with reference types too (which are always nullable).

๐ŸŒ
Reddit
reddit.com โ€บ r/programminglanguages โ€บ thoughts on the null coalescing (??) operator precedence?
r/ProgrammingLanguages on Reddit: Thoughts on the Null Coalescing (??) operator precedence?
April 30, 2024 -

Many languages have a "null-coalescing" operator: a binary operator used to unwrap an optional/nullable value, or provide a "default" value if the LHS is null/none. It's usually spelled ?? (as in Javascript, Swift, C#, etc.).

I'm pondering the precedence of such an operator.

Why not just use no precedence? Parenthesis! S-expressions! Polish!

All interesting ideas! But this post will focus on a more "C-style" language perspective.


As for ??, it seems like there's a bit of variety. Let's start with a kind of basic operator precedence for a hypothetical C-style statically typed language with relatively few operators:

precoperatorstypes
1Suffixes: a()-> any type
2High-prec arithmetic: a * binteger, integer -> integer
3Low-prec arithmetic: a + binteger, integer -> integer
4Comparisons: a == binteger, integer -> boolean
5Logic: a && bboolean, boolean -> boolean

There are subtly differences here and there, but this is just for comparisons. Here's how (some) different languages handle the precedence.

  • Below #5:

  • C#

  • PHP

  • Dart

  • Equal to #5

  • Javascript (Kinda; ?? must be disambiguated from && and ||)

  • Between #3 and #4:

  • Swift

  • Zig

  • Kotlin

So, largely 2 camps: very low precedence, or moderately low. From a brief look, I can't find too much information on the "why" of all of this. One thing I did see come up a lot is this: ?? is analogous to ||, especially if they both short-circuit. And in a lot of programming languages with a looser type system, they're the same thing. Python's or comes to mind. Not relevant to a very strict type system, but at least it makes sense why you would put the precedence down that. Score 1 for the "below/equal 5" folk.


However, given the divide, it's certainly not a straightforward problem. I've been looking around, and have found a few posts where people discuss problems with various systems.

  • https://forums.swift.org/t/nil-coalescing-operator-precedence/2954

  • https://www.codeproject.com/Tips/721145/Beware-The-null-coalescing-operator-is-low-in-the

These seem to center around this construct: let x = a() ?? 0 + b() ?? 0. Operator precedence is largely cultural/subjective. But if I were a code reviewer, attempting to analyze a programmer's intent, it seems pretty clear to me that the programmer of this wanted x to equal the sum of a() and b(), with default values in case either were null. However, no one parses ?? as having a higher precedence than +.

This example might be a bit contrived. To us, the alternate parse of let x = a() ?? (0 + b()) ?? 0 because... why would you add to 0? And how often are you chaining null coalescing operators? (Well, it can happen if you're using optionals, but it's still rare). But, it's a fairly reasonable piece of code. Those links even have some real-world examples like this people have fallen for.


Looking at this from a types perspective, I came to this conclusion; In a strongly-typed language, operator precedence isn't useful if operators can't "flow" from high to low precedence due to types.

To illustrate, consider the expression x + y ?? z. We don't know what the types of x, y, and z are. However, if ?? has a lower precedence than +, this expression can't be valid in a strictly typed language, where the LHS of ?? must be of an optional/nullable type.

If you look back at our hypothetical start table, you can see how operator types "flow" through precedence. Arithmetic produces integers, which can be used as arguments to comparisons. Comparisons produce booleans, which can be used as arguments to logical operators.

This is why I'd propose that it makes sense for ?? to have a precedence, in our example, between 1 and 2. That way, more "complex" types can "decay" though the precedence chain. Optionals are unwrapped to integers, which are manipulated by arithmetic, decayed to booleans by comparison, and further manipulated by logic.


Discussion questions:

  1. What are some reasons for choosing the precedence of ?? other than the ones discussed?

  2. Have any other languages done something different with the precedence, and why?

  3. Has anyone put the precedence of ?? above arithmetic?

Thanks!

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c# โ€บ null-coalescing-operator-in-c-sharp
Null-Coalescing Operator in C# - GeeksforGeeks
October 23, 2025 - The null-coalescing operator (??) in C# is used to handle null values efficiently by providing a fallback or default value.
๐ŸŒ
Google Groups
groups.google.com โ€บ a โ€บ isocpp.org โ€บ g โ€บ std-proposals โ€บ c โ€บ uNYpDyi7VSA
Null coalescing operator
Consider the following conditional operator use case: Test* get_test() { /*...*/ } Test* test = (get_test() != nullptr) ? get_test() : new Test(); If `get_test()` has side effects, we may have a problem. We would use the following instead. Test* test = get_test(); test = test != nullptr ? test : new Test(); We could instead use a shortcut without side effect, the null coalescing operator ??.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ csharp โ€บ language-reference โ€บ operators โ€บ member-access-operators
Member access and null-conditional operators and expressions: - C# reference | Microsoft Learn
to specify an alternative expression to evaluate in case the result of a null-conditional operation is null. If a.x or a[x] is of a non-nullable value type T, a?.x or a?[x] is of the corresponding nullable value type T?. If you need an expression ...
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @leroyleowdev โ€บ c-null-coalescing-operator-97d3e2048610
C# Null Coalescing (??) operator. The null coalescing operator (??) in C#โ€ฆ | by Leroy Leow | Medium
July 19, 2024 - The null coalescing operator (??) in C# is a convenient way to handle null values in expressions. It checks whether its left-hand operand is null and, if so, evaluates and returns the right-hand operand instead.
๐ŸŒ
C# Corner
c-sharpcorner.com โ€บ blogs โ€บ null-conditional-operator-and-null-coalescing-operator
Null Conditional Operator And Null Coalescing Operator
April 12, 2023 - the ?? operator is called the Null Coalescing Operator, and it allows you to provide a default value for a nullable type or a null value. It checks if the left operand is null, and if it is, it returns the right operand.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ shows โ€บ on-dotnet โ€บ c-language-highlights-null-coalescing-operator
C# Language Highlights: Null Coalescing Operator | Microsoft Learn
The null coalescing operator in C# is a really handy feature. Learn how it works in this short video from James and Maira Useful Links?? and ??= operators (C# reference)First steps with C#
๐ŸŒ
C# Corner
c-sharpcorner.com โ€บ UploadFile โ€บ ff2f08 โ€บ null-coalescing-operator-in-C-Sharp16
Null Coalescing (??) Operator in C#
February 7, 2024 - Explore the Null Coalescing (??) Operator in C#, a versatile tool for handling null values. Learn its syntax, usage, and benefits in providing default values and simplifying error handling in conditional ...
๐ŸŒ
Built In
builtin.com โ€บ articles โ€บ null-coalescing-operator-c
Null Coalescing Operator (??) in C# Explained | Built In
In the above code, the "?? " operator performs a null check on the left-hand side variable (in this case, "name"). If the left-hand side is null, the operator returns the right-hand side value ("Anonymous" in this case) and assigns it to the left-hand side variable. The null coalescing operator can be used with any nullable type, including reference types and nullable value types.
Top answer
1 of 4
16

I use the null coalescing operator all of the time. I like the conciseness of it.

I find this operator to be similar in nature to the ternary operator (A ? B : C). It takes a little practice before the reading of it is second nature, but once you're used to it I feel readability improves over the longhand versions.

Also, the situation you describe is only one scenario where the operator is useful. It's also handy to replace constructs like this:

if (value != null)
{
    return value;
}
else
{ 
    return otherValue;
}

or

return value != null ? value : otherValue;

with

return value ?? otherValue;
2 of 4
2

What I'm wondering about specifically is using the operator to set an object to itself unless it's null.

The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.

myObject = myObject ?? new myObject(); - instantiate default value of object

More details and code sample about null-coalescing operator - MSDN article.

If you check for more than nullable condition, as an alternative you may use Ternary operator.

Ternary operator

You may also look at ?: Operator. It is called Ternary or conditional operator. The conditional operator (?:) returns one of two values depending on the value of a Boolean expression.

A nullable type can contain a value, or it can be undefined. The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type. If you try to assign a nullable value type to a non-nullable value type without using the ?? operator, you will generate a compile-time error. If you use a cast, and the nullable value type is currently undefined, an InvalidOperationException exception will be thrown.

A code example from MSDN - ?: Operator (C# Reference):

int? input = Convert.ToInt32(Console.ReadLine());
string classify;

// ?: conditional operator.
classify = (input.HasValue) ? ((input < 0) ? "negative" : "positive") : "undefined";
๐ŸŒ
IronPDF
ironpdf.com โ€บ ironpdf blog โ€บ .net help โ€บ null coalescing operator c#
Null Coalescing Operator C# (How It Works For Developers)
June 23, 2025 - The Null Coalescing Operator (??) or Null Conditional Operator is a concise and powerful binary operator in C# designed to streamline null value handling
๐ŸŒ
Ivan Kahl's Blog
blog.ivankahl.com โ€บ csharp-null-conditional-and-null-coalescing-operators-explained
C# Null-Conditional (?.) & Null-Coalescing (??) Operators Explained
September 10, 2025 - The null-conditional operator (?.) lets you safely access members of potentially null objects, while the null-coalescing operator (??) provides a concise way to supply default values when encountering nulls.
๐ŸŒ
Quora
quora.com โ€บ How-is-null-coalescing-operator-implemented-in-C
How is null coalescing operator implemented in C#? - Quora
Answer (1 of 5): The null coalescing operator ([code ]??[/code]) is a binary operator in C# that evaluates and checks if the first operand is [code ]null[/code]. If it isnโ€™t, then the value of the first operand is the value of the operation; ...
๐ŸŒ
Salesforce Developers
developer.salesforce.com โ€บ docs โ€บ atlas.en-us.apexcode.meta โ€บ apexcode โ€บ langCon_apex_NullCoalescingOperator.htm
Null Coalescing Operator | Apex Developer Guide | Salesforce Developers
The ?? operator returns its right-hand side operand when its left-hand side operand is null. Similar to the safe navigation operator (?.), the null coalescing operator (??) replaces verbose and explicit checks for null references in code.
๐ŸŒ
DZone
dzone.com โ€บ articles โ€บ nullable-types-and-null-coalescing-operator-in-c
Nullable Types and Null Coalescing Operator in C#
June 15, 2018 - The C# Null Coalescing Operator (??) is a binary operator that simplifies checking for null values.
๐ŸŒ
DEV Community
dev.to โ€บ pushpk โ€บ using-null-conditional-and-coalescing-operators-in-c-1186
Using NULL Conditional and Coalescing Operators in C# - DEV Community
June 15, 2020 - It returns the left-hand operand if the operand is not null; otherwise, it returns the right operand. In cases where a statement could return null, the null-coalescing operator can be used to ensure a reasonable value gets returned.