Alex Rivera | Logout

C# - What does the Assert() method do? Is it still useful?

Asked 2008-10-02T17:24:28.407
199

I am debugging with breakpoints and I realize the assert call? I thought it was only for unit tests. What does it do more than breakpoint? Since I can breakpoint, why should I use Assert?

Edit
Report

2 Answers

44

You should use it for times when you don't want to have to breakpoint every little line of code to check variables, but you do want to get some sort of feedback if certain situations are present, for example:

Debug.Assert(someObject != null, "someObject is null! this could totally be a bug!");
answered 2008-10-02T17:31:13.927
10

Assert allows you to assert a condition (post or pre) applies in your code. It's a way of documenting your intentions and having the debugger inform you with a dialog if your intention is not met.

Unlike a breakpoint, the Assert goes with your code and can be used to add additional detail about your intention.

answered 2008-10-02T17:26:03.020

Your Answer