I was looking at a standards website and found this code:
Which seemed very odd to me. I always check for null likeCode:using System.Diagnostics; object GetObject() {...} object someObject = GetObject(); Debug.Assert(someObject != null);
but here they use Debug.Assert. Does that seem odd or normal to you?Code:if (!someObject != null)
The sample code isn't actually checking for null - they're asserting it. An assert only fires under Debug builds (technically, when the DEBUG compiler constant is defined) and displays a message/outputs to any listeners a callstack if the Assert fails. This can aid in debugging.
In Release builds, the assert should fail but will really have no behaivor (because the DEBUG constant is not defined). Note that the Visual C++ and JScript.NET compilers ignore this, and have the same behaivor in both Release and Debug.
Ok, I understand now. What does this assert do?
Say
What will display on the debug window?Code:object someObject = GetObject(); someObject = null; Debug.Assert(someObject != null);
By default, in a Debug build, that'll show a message box (in a UI app) and a stack trace.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks