-
Today I tried to do the following as an experiment: public async Task<bool> CreateAsync(int id)
{
if (id == default) throw new ArgumentDefaultException(nameof(id));
if (id < default) throw new ArgumentOutOfRangeException(nameof(id));
//...
} But received the following error:
Why is this? I assumed the compiler was replacing |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The |
Beta Was this translation helpful? Give feedback.
The
default
literal keyword needs to be "target-typed" in order to figure out what type it should represent. We don't have any rules for inferring target-types for one side of an operator like>
or<
from the other side, soid
cannot informdefault
what type it should be. I would also say that, IMO, this type of code relatively unclear, and it's much better to just use 0.