The following code is being executed in a highly threaded environment.
1. Why does the Debug.Assert statement sometimes fail?
2. What can be done to stop this from occurring?
public class Warehouse
private int stockCount = 0;
public void DecrementStock ()
if ( stockCount > 0 )
Debug.Assert ( stockCount >= 0 )
public void IncrementStock()
The answer is a beauty:
Remove the code in decrementing the stock… this would make the condition of Debug.Assert to always true.
What a great answer - if in doubt, delete the code. As long as the Assert validates then the system must be correct. Why, even the unit tests would all work then. Genius!!