2

I have seen couple of posts regarding usage of Debug.Assert in C#. But I still have one doubt, may be its repeated, but I need to ask.

Is there a strict rule that Debug.Assert should be used only for checking members of a class or used to check parameters to a public method?

Or Can i Use Debug.Assert where ever I want, and to check whichever condition?

Thanks sandeep

MSTdev
  • 4,507
  • 2
  • 23
  • 40
Sandepku
  • 861
  • 14
  • 31

2 Answers2

5

Is there a strict rule that Debug.Assert should be used only for checking members of a class or used to check parameters to a public method?

Do not use Debug.Assert() to check parameters to a public method. Parameters should be checked in both debug and release builds.

You should use an explicit if followed by thowing ArgumentNullException, ArgumentOutOfRangeException or ArgumentException for invalid parameters.

Alternatively, use Code Contracts to express the parameter preconditions using Contract.Requires().

For further reference, see this thread: When should I use Debug.Assert()?

Other than that, then you can use Debug.Assert() wherever you want, but be aware that it might take a little more setting up for Asp.Net: Is it worth using Debug.Assert in ASP.NET?

Also see here: http://gregbeech.com/blog/how-to-integrate-debug-assert-with-your-asp-net-web-application

Community
  • 1
  • 1
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
4

You can use it wherever you want. Just be aware that it's a debug check. So it's checked only at development time while you test it. If you need your program to actually change behaviour based on a condition, you still need additional ifs.

Read the coding guidelines over at Microsoft and try to use tools like the static code analysis or Visual Studio (formerly FxCop) and StyleCop to have an automated way of checking your code quality and common mistakes.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • I am concerned about a case, where I have parameters to a method, if they end up being null, should I throw an exception in that case ? if its a public method or even in case of private method, indicating invalidoperation? I am doing Debug.Assert,but in reality should there be exception cases everywhere then? – Sandepku May 23 '13 at 05:55
  • 2
    @Sandepku You should throw exceptions (`ArgumentNullException`, `ArgumentOutOfRangeException` or `ArgumentException`) for invalid parameters, or use `Contract.Requires()`. Note that if you want to check other program state for release builds *and* debug builds, you can use `Trace.Assert()` (but don't use that for argument checking!) – Matthew Watson May 23 '13 at 06:07
  • @MatthewWatson if I really have exceptions thrown, then why do I need to use Debug.Assert ? I am ok if the program stops its execution, even if its debug mode.! – Sandepku May 23 '13 at 06:12
  • 2
    @Sandepku You should never use `Debug.Assert()` to check parameters, and you are right - if you are already using an `if` to check them and throw exceptions, you do *not* need `Debug.Assert()`. – Matthew Watson May 23 '13 at 06:13