© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
65 replies
Ruttie

✅ `??=` and `??`

This question may not fit here because it is not a question about C# but more about why it is implemented in such a way, but I'll ask anyways:

Why is
item ??= item2
item ??= item2
implemented as
            if ((object)item == null)
            {
                item = item2;
            }
            if ((object)item == null)
            {
                item = item2;
            }
instead of
            if (item == null)
            {
                item = item2;
            }
            if (item == null)
            {
                item = item2;
            }
?
The cast makes it so if the
==
==
operator is changed by the class, it will not use said operator, instead using the default
==
==
operator of
object
object
.
This makes it so that in unity for example, where the
==
==
operator is changed by all unity objects, the
??=
??=
operator cannot be used.

My other question is, does the
??
??
operator also cast to
object
object
before null checking, or does it use something else completely, like pattern matching? I've used https://sharplab.io to see the lowering of
??
??
, but it doesn't appear to be lowered.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Efcore Dynamic Linq, AND and OR
C#CC# / help
4mo ago
Registering IDbCommand and autowiring and testing
C#CC# / help
3y ago
Navigation and Dialogs with MVVM and WPF
C#CC# / help
2y ago
Encryption and Decryption for Register and Login
C#CC# / help
3y ago