They are effectively two different Asserts. The Assert you use in unit tests is for triggering a test failure (and of course, they are always tested). The Asserts you use in other code are a separate function (System.Diagnostics.Debug.Assert()
), and are used as a helping tool during development, to alert you if expected conditions are not satisfied. However, these assertions are only tested in debug builds. If you make a release build, the assert will have no effect. So it is not a general error-handling tool, and you should not plan on it to catch errors.
It is simply to catch logic errors during testing and development, to tell you if your assumptions hold. And as such, yes, it is very useful for testing pre- and postconditions.
Note that this kind of Assert is a bit contentious, because it may become a crutch you end up using instead of proper error handling, and since it has no effect in release builds, you may end up releasing software with nonexistent error handling. (For example, don't use Assert to check that a file exists. It's a real error that can actually occur in the real world, so it needs real-world error handling.) But personally, I think it's a very useful tool, for testing pre- and postconditions without having to worry about the performance overhead (since the test gets removed in release builds, it is effectively free, and it triggers the debugger when the assertion fails, rather than just throwing an exception)