Click here to Skip to main content
12,953,230 members (46,780 online)
Click here to Skip to main content
Add your own
alternative version


Posted 18 Nov 2010

Back to Basics – Null-Coalescing Operator

, 18 Nov 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Back to Basics – Null-Coalescing Operator Yesterday during an EF4 course that I’m giving at a customer I showed an example for a property that is set using the null-coalescing operator.

Yesterday during an EF4 course that I’m giving at a customer I showed an example for a property that is set using the null-coalescing operator. Since some of the students asked me what is this operator, I gave a small explanation and thought that it’s something that I can share here in the blog. So here it goes…

Null-Coalescing Operator

The null-coalescing operator or ?? can be very useful when you want to check nullity of a reference type or nullable types. When it is used, it returns the left-hand side of the operator if it is not null. If the left-hand side is null, it returns the right side. Here is an example of how you can use the operator in a property to get a lazy initialization of the property: 

public ObjectSet<ContactDetails> ContactDetails      
    get { return _contactDetails  ?? (_contactDetails = CreateObjectSet<ContactDetails>(
        "ContactDetails")); }      
private ObjectSet<ContactDetails> _contactDetails;

This property is taken from an Entity Framework generated ObjectContext. When the getter is used, first there is a check whether _contactDetails is null and if not the getter will return its value. If the _contactDetails is null then the right side of the operator will be evaluated and the CreateObjectSet method will run and create the ContactDetails object set. The result of the evaluation will be returned by the operator.

There are other scenarios to use the null-coalescing operator such as checking whether a nullable type is null in order to set its value if exists in a simple type and if not to set a default value. For example:

int? x = null;      
int y = x ?? -1;

The example shows the use of the operator in order to determine whether x is null. in the example y will be equal to x, unless x is null. If x will be null y will be set to -1.


Lets sum up, the null-coalescing operator is a very simple operator that can be very helpful in null checking scenarios.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website:

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 18 Nov 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid