Consider the code below:
#if DEBUG
if (Systems.Contains(system))
throw new InvalidOperationException("System already registered");
#endif
Debug.Assert(!Systems.Contains(system), "System already registered");
Previously I used to do the former, I’ve since discovered Debug.Assert.
Is there a reason why I should not always prefer Debug.Assert?
- It only exists in debug code (it has the attribute
[Conditional("DEBUG")]). - It seems to me to be more suited to my intention (code sanity checks, rather than raising exceptions to be handled later).
- It’s less code to write.
You can always use Debug.Assert(), because this class is complied with the DEBUG conditional as well: