Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

An Oddity of the ?? Operator (C#)

4.68/5 (7 votes)
20 Jan 2022CPOL2 min read 19.1K  
The ?? operator is odd when combined with exceptions - It helps assignments but not non-assignments. Why?
This is a quick complaint of a feature that I consider very annoying which Visual Studio is trying to make us use, when the feature isn't really making code more readable, it is just making it more compact in just half the situations.

This is how we would probably do things in old C# code:

C#
if (input == null)
  throw new ArgumentNullException("input");

_input = input;

Now, we can reduce the entire thing to just:

C#
_input = input ?? throw new ArgumentNullException("input");

The odd thing is that if I am not assigning input to _input, the old code will just lose code while the new code needs to go back to the old code, becoming:

C#
if (input == null)
  throw new ArgumentNullException("input");

With the new ?? operator, we can't just remove the _input = . That will simply not compile.

Isn't that odd?

Why can't just input ?? throw new ArgumentNullException("input"); mean the same as...

C#
if (input == null)
  throw new ArgumentNullException("input");

... when we are not assigning anything to a variable?

I understand it would make no sense for non-assigments if we were just using ?? to read a variable, but when there is an exception being thrown or a method being called, why not?

It should work as an "if replacement" in those circumstances too, don't you think?

Expressions vs Statements - Does it matter?

I got an answer that the reason ?? works in one case and not the other is because it is an operator and only works as part of expressions, not statements.

Although there is some truth in there, the ?. is also an operator and works on bare statements. So, assuming throw is usually a statement, operator ?? should possibly work with it too.

Also I see that even in MSDN (for example this one about async enumerators) they compare what we write, and what the code is really compiled into (so, we can say that one expression is just a synctactic sugar for another).

That's why I will do this comparison:

C#
obj ?? ObjIsNull(); // Makes sense
obj ?? throw new Exception(); // Makes sense
obj ?? x; // Doesn't make any sense.

var y = obj ?? x; // This one works right now.

All of those can actully be seen as synctactic sugar for:

C#
if (obj == null)
  ObjIsNull(); // Should compile fine.

if (obj == null)
  throw new Exception(); // Should compile fine.

if (obj == null)
  x; // Should cause a compile-time error.


var y = obj;
if (y == null)
  y = x; // Should also work fine.

So, I believe that independenty if we can justify why it doesn't work in statements, the operator ?? is perfectly applicable in statements when we have method calls or when throwing exceptions.

This is Not an Article

Just to make it obvious, this is not an article, that's why I didn't put introduction, background and things like that. Also, I will not discuss coding styles, like using { and } on every if (which actually makes the presented problem even worse) or using the nameof() keyword.

This is just a "quick complaint" of a feature that I consider very annoying that Visual Studio is trying to make us use, when the feature isn't really making code more readable, it is just making it "more compact" in just half the situations.

History

  • 20th January, 2022: Added Expression vs Statements topic
  • 19th January, 2022: Initial version

License

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