free geoip C# Coalesce Operator -- Underlying Type Not Tested For Null? - protected virtual void jaysonBlog
in

jaysonKnight.com

Welcome to my corner of the internet

protected virtual void jaysonBlog

A conduit to the voices in my head

C# Coalesce Operator -- Underlying Type Not Tested For Null?

I recently came across an irk with the new C# coalesce operator (??) which, if done by design, doesn't really make a whole lot of sense to me.  I couldn't find any documentation either confirming or denying that this is by design...if it's by design I'm sure there is a good reason for doing so, if not then this behavior

Consider the following pseudo-code:

MyClass myClass = new MyClass(); // do some stuff with myClass, then time to do some cleanup such as persisting a string property SettingsClass.SettingNameString = myClass.NameString ?? someDefaultString;

If myClass is null, this will fail with a null reference exception; the coalesce operator doesn't check the underyling type first.  Of course this can be expressed easily with the ternary operator, i.e. myClass != null ? myClass.NameString : someDefaultString, but it would make more sense for the coalesce operator to walk the type chain testing for nulls along the way.  If this is indeed by design, anyone care to explain why?

Share this post: Submit C%23+Coalesce+Operator+--+Underlying+Type+Not+Tested+For+Null%3f to Technorati | Submit C%23+Coalesce+Operator+--+Underlying+Type+Not+Tested+For+Null%3f to del.icio.us | Submit C%23+Coalesce+Operator+--+Underlying+Type+Not+Tested+For+Null%3f to digg.com | Submit C%23+Coalesce+Operator+--+Underlying+Type+Not+Tested+For+Null%3f to reddit.com | Submit C%23+Coalesce+Operator+--+Underlying+Type+Not+Tested+For+Null%3f to DotNetKicks | Add C%23+Coalesce+Operator+--+Underlying+Type+Not+Tested+For+Null%3f to Live Bookmarks
Only published comments... Jan 19 2007, 02:36 AM by Jayson Knight

Comments

 

Haacked said:

I think operator precedence is the reason.

Before the ?? even gets executed, it has to evaluate the myClass.NameString property. But if myClass is null, that will throw an exception before it even gets to the null coalescing operator.

What you are asking the ?? operator to do is wrap the left hand side with a try/catch exception looking for and swallowing a NullReferenceException. Probably not a good idea.

January 19, 2007 3:26 AM
 

Rich Mercer said:

I would say this is how I would expect it to work. In the same way, if you tried the following, it would still throw an exception and really, all the coalesce is doing is creating a shorthand way to do this if statement.

MyClass myClass = new MyClass();

if (myClass.NameString != null)  { // Do something }

January 19, 2007 7:40 AM
 

Rich Denis said:

Even then the probelm remains that if myClass is null then you are asking the compiler to know that you want it to be null.  what you need is the null coalescing || operator ;).

January 19, 2007 7:46 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About Jayson Knight

Jayson Knight was clueless to the computer programming world until he took a C++ class in college. The rest is proverbial history. He has been building applications targeting the .Net framework for 7 years, focusing mainly on internet technologies and database driven web application development.

Most recently he left the world of Corporate IT to finish up his degree in Chemistry, with an eye on Medical School and an Anesthesiology residency program. Read this post for more information.

He is also a Community Server MVP: Community Server is the software that runs this site, plus many others on the web. For more information, check out http://csmvps.com.

When he finds time to pry himself away from his computer and university studies, he can be found on the mountain bike trails when it's warm, and on the ski slopes when it's cold.

Copyright © :: JaysonKnight.com
External Content © :: Respective Authors

Terms of Service/Privacy Policy