Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top