Test code vs. real code – how to make them different?
When you are testing something you’ll want the code to run in a particular way, but when you are dealing with the real thing – in “production” mode – then you might not want to have the test code there as well. How can you manage this in c#?
You can add a statement in your code which only compiles in the debug mode. So when you take it to production, all that test code which would be compiled when debugging would be completely eliminated.
#if DEBUG
// this code will run only when debugging.
#endif
Or you can add an attribute:
[Conditional("DEBUG")]
// add it to what you want available only when debugging.
It’s as simple as that! Hope this helps.
Written on February 28, 2017