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: 
|

|

|

|

|
