考虑以下守则:
#if DEBUG
if (Systems.Contains(system))
throw new InvalidOperationException("System already registered");
#endif
Debug.Assert(!Systems.Contains(system), "System already registered");
此前,我曾习惯于做过,我曾发现过<代码>。 Debug.Assert 。
我为什么不总是喜欢<代码>? 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.