Information Technology Reference
In-Depth Information
designers have responded by creating better tools to produce different
machine code for different environments. C# has added the Conditional
attribute to indicate whether a method should be called based on an envi-
ronment setting. It's a cleaner way to describe conditional compilation
than #if/#endif . The compiler understands the Conditional attribute,
so it can do a better job of verifying code when conditional attributes are
applied. The conditional attribute is applied at the method level, so it
forces you to separate conditional code into distinct methods. Use the
Conditional attribute instead of #if/#endif blocks when you create con-
ditional code blocks.
Most veteran programmers have used conditional compilation to check
pre- and post-conditions in an object. You would write a private method
to check all the class and object invariants. That method would be condi-
tionally compiled so that it appeared only in your debug builds.
private void CheckStateBad()
{
// The Old way:
#if DEBUG
Trace .WriteLine( "Entering CheckState for Person" );
// Grab the name of the calling routine:
string methodName =
new StackTrace ().GetFrame( 1 ).GetMethod().Name;
Debug .Assert(lastName != null ,
methodName,
"Last Name cannot be null" );
Debug .Assert(lastName.Length > 0 ,
methodName,
"Last Name cannot be blank" );
Debug .Assert(firstName != null ,
methodName,
"First Name cannot be null" );
Debug .Assert(firstName.Length > 0 ,
methodName,
"First Name cannot be blank" );
Search WWH ::




Custom Search