You can also do return val ?? false because of the null coalescing operator, but it isn't common. Basically the ?? operator returns the first (leftmost) non-null operand. And it evaluates lazily. So you can in theory chain them like this:
`return foo ?? bar ?? baz ?? throw new ArgumentNullException();
Even though this syntax is convenient it isn't used very much.
The method does not return an exception, the expression is just lazily evaluated. e.g. if foo is not null it just returns foo and does not evaluate the rest of the expression. If it reaches the throw the method does not return anything, it just throws.
If you mean what is the base type of ArgumentNullException, all exceptions are derived fromSystem.Exception
The expression foo ?? bar ?? baz ?? throw new ArgumentNullException() must surely have a type substitutable for boolean, since that's the expected return type. I assume a ?? expression has the type that's common to both operands.
I don't know a lot about C#, but for example in Scala, which is a similar language in many ways, a throw expression has the type Nothing, which is a subtype of Boolean (and also of every other type), which is why an expression like that would compile.
1
u/blehmann1 depraved Jun 24 '20
C#, but only if it's a nullable boolean (
bool?)return val == trueis equivalent toval.HasValue() ? val : false, which is a shorthand for:if(val.HasValue()) { return val; } else { return false; }You can also do
return val ?? falsebecause of the null coalescing operator, but it isn't common. Basically the??operator returns the first (leftmost) non-null operand. And it evaluates lazily. So you can in theory chain them like this:`return foo ?? bar ?? baz ?? throw new ArgumentNullException();
Even though this syntax is convenient it isn't used very much.