Assertions in Visual Studio debugger-managed code
)
Assertions in Visual Studio debugger-managed code
Assertion (orAssertStatement) test yourAssertThe condition specified by the statement parameters. If this condition is calculated as true, no operation is performed. If this condition is calculated as false, the assertion fails. If the program is running in the debugging version, the program enters the interrupt mode.
In Visual Basic and visualC #From debug orTraceUsed inAssertMethods, both of which are in the system. Diagnostics namespace. BecauseDebugClass methods are not included in the program's "release" version, so they do not increase the size of the published code or reduce its speed.
C ++ does not supportDebugClass method. Use Conditional compilationTraceClass (such as # ifdef debug...) can achieve the same effect. # Endif.
Debug.
AssertMethod
You can use system. Diagnostics. debug.Assert(System. Boolean) method test condition. If the code is correct, the condition should be true. For example, suppose you have compiled an integer division function. According to mathematical rules, the divisor cannot be zero. You can use assertions to test this condition:
[Visual Basic]
Function integerdivide (byval dividend as integer, byval divisor as integer) as integer debug.Assert(Divisor <> 0) return CINT (dividend/divisor) end Function
[C #]
Int integerdivide (INT dividend, int divisor) {debug.Assert(Divisor! = 0); Return (dividend/divisor );}
When the code is run in the debugger, the asserted statement is calculated, but the comparison is not performed in the released version, so there is no additional system overhead.
The following is another example. You have a class that implements a checkdemand deposit account, as shown below:
[Visual Basic]
Dim amount, balance as double balance = savingsaccount. Balance debug.Assert(Amount <= balance) savingsaccount. Withdraw (amount)
[C #]
Float balance = savingsaccount. Balance; debug.Assert(Amount <= balance); savingsaccount. Withdraw (amount );
Before you withdraw money from this account, make sure that the account balance is greater than the amount to be withdrawn. You can write assertions to check the balance:
[Visual Basic]
Dim amount, balance as double balance = savingsaccount. BalanceTrace.Assert(Amount <= balance) savingsaccount. Withdraw (amount)
[C #]
Float balance = savingsaccount. balance;Trace.Assert(Amount <= balance); savingsaccount. Withdraw (amount );
Note: When you create a release version of the CodeSystem. Diagnostics. debug.Assert(System. Boolean)Method call will disappear. This means that the call to check the balance in the released version will disappear. To solve this problem, you should use system. diagnostics.Trace.Assert(System. Boolean) replaceSystem. Diagnostics. debug.Assert(System. Boolean):
And pairSystem. Diagnostics. debug.Assert(System. Boolean)Different callsSystem. diagnostics.Trace.Assert(System. Boolean)Will increase the overhead of the released version.
Debug.
AssertSide effects
UseSystem. Diagnostics. debug.Assert(System. Boolean)Make sure thatAssertNo code in will change the program results (if removedAssert). Otherwise, you may introduce a bug that only appears in the release version of the program. Be especially careful with assertions that contain functions or process calls, as shown in the following example:
[Visual Basic]
'Unsafe code debug.Assert(MEAs (I) <> 0)
[C #]
// Unsafe code debug.Assert(MEAs (I )! = 0 );
At first glance, useSystem. Diagnostics. debug.Assert(System. Boolean)It seems safe, but it is assumed that the counter will be updated every time the MEAs function is called. When a release version is generated, the counter will not be updated because the call to MEAs is eliminated. This is an example of a function with "Side effects. Removing calls to functions with side effects can result in a bug that only appears in the released version. Do not place function calls inSystem. Diagnostics. debug.Assert(System. Boolean)Statement. Use temporary variables:
[Visual Basic]
Temp = MEAs (I) debug.Assert(Temp <> 0)
[C #]
Temp = MEAs (I); debug.Assert(Temp! = 0 );
Even in useSystem. diagnostics.Trace.Assert(System. Boolean)You also need to avoid placing function callsAssertStatement. Such a call should be safe because it is not eliminated in the released version.System. diagnostics.Trace.Assert(System. Boolean)Statement. However, if you are used to avoiding such a structure, useSystem. Diagnostics. debug.Assert(System. Boolean)The possibility of making mistakes is very small.
TraceAnd debug requirements
If you use the Visual Studio Wizard to create a project, the "publish" configuration and "debug" configuration are defined by default.TraceSymbol. By default, only debug symbols are defined in the debug version.
OtherwiseTraceThe program must place one of the following code at the top of the source file:
Or, the program must beTraceOption:
If you wantC #Or the debug method is used in the Visual Basic release version. The debug symbol must be defined in the "release" configuration.
C ++ does not supportDebugClass method. Use Conditional compilationTraceClass (such as # ifdef debug...) can achieve the same effect. # Endif can define these symbols in the <project> properties page dialog box. For more information, see change project settings for Visual Basic Debugging configuration or change project settings for C or C ++ debugging configuration .)
AssertParameters
System. diagnostics.Trace.Assert(System. Boolean)AndSystem. Diagnostics. debug.Assert(System. Boolean)You can set up to three parameters. The first parameter is mandatory and is the condition to be checked. If you use only one parameter to callSystem. diagnostics.Trace.Assert(System. Boolean)OrSystem. Diagnostics. debug.Assert(System. Boolean),AssertThe method checks the conditions, and if the result is false, the call stack content is output to the "output" window. The following example showsSystem. diagnostics.Trace.Assert(System. Boolean)AndSystem. Diagnostics. debug.Assert(System. Boolean):
[Visual Basic]
Debug.Assert(Stacksize> 0)Trace.Assert(Stacksize> 0)
[C #]
Debug.Assert(Stacksize> 0 );Trace.Assert(Stacksize> 0 );
The second and third parameters (if any) must be strings. If the call has two or three parametersSystem. diagnostics.Trace.Assert(System. Boolean)OrSystem. Diagnostics. debug.Assert(System. Boolean)The first parameter is a condition. This method checks this condition. If the result is false, the second and third strings are output. The following example demonstrates system. Diagnostics. debug.Assert(System. Boolean, system. String) and system. diagnostics.Trace.Assert(System. Boolean, system. String) When two parameters are used:
[Visual Basic]
Debug.Assert(Stacksize> 0, "Out of stack space ")Trace.Assert(Stacksize> 0, "Out of stack space ")
[C #]
Debug.Assert(Stacksize> 0, "Out of stack space ");Trace.Assert(Stacksize> 0, "Out of stack space ");
The following example showsAssertAndAssert:
[Visual Basic]
Debug.Assert(Stacksize> 0, "Out of stack space. bytes left:", format (size, "G "))Trace.Assert(Stacksize> 0, "Out of stack space. bytes left:", format (size, "G "))Trace.Assert(Stacksize> 0, "Out of stack space. bytes left:", "inctemp failed on third call ")
[C #]
Debug.Assert(Stacksize> 100, "Out of stack space", "failed in inctemp ");Trace.Assert(Stacksize> 0, "Out of stack space", "failed in inctemp"); customAssertAction
If you run an application in user interface mode, when the condition fails,AssertThe "assertion failed" dialog box is displayed. Operations that occur when an assertion fails are controlled by the listeners or listeners attributes.
You canListenersSet to add a tracelistener objectListenersSet RemovalTracelistenerOr rewrite the existingTracelistenerThe system. Diagnostics. tracelistener. Fail (system. String) method of is used to customize the output behavior and make the behavior different.
For example, you can rewriteSystem. Diagnostics. tracelistener. Fail (system. String)Instead of displaying the "assertion failure" dialog box.
To customize the output in this way, the program must contain the listener andTracelistenerInherit and override itsSystem. Diagnostics. tracelistener. Fail (system. String)Method.
For more information, see trace listeners.
Set assertions in the configuration file
You can set assertions in the program configuration file and code. For more information, seeSystem. diagnostics.Trace.Assert(System. Boolean)OrSystem. Diagnostics. debug.Assert(System. Boolean).