I have a number of methods doing next:
var result = command.ExecuteScalar() as Int32?;
if(result.HasValue)
{
return result.Value;
}
else
{
throw new Ex
The reason you can't do:
return command.ExecuteScalar() as Int32? ?? throw new Exception();
Is because throwing an exception is a statement, not an expression.
If you're just looking to shorten the code a little bit, perhaps this:
var result = command.ExecuteScalar() as Int32?;
if(result.HasValue) return result;
throw new Exception();
No need for the else.