Information Technology Reference
In-Depth Information
Debug .Assert(firstName != null ,
methodName,
"First Name cannot be null" );
Debug .Assert(firstName.Length > 0 ,
methodName,
"First Name cannot be blank" );
Trace .WriteLine( "Exiting CheckState for Person" );
#endif
}
The Conditional attribute can be applied only to entire methods. In addi-
tion, any method with a Conditional attribute must have a return type of
void. You cannot use the Conditional attribute for blocks of code inside
methods or with methods that return values. Instead, create carefully con-
structed conditional methods and isolate the conditional behavior to those
functions. You still need to review those conditional methods for side
effects to the object state, but the Conditional attribute localizes those
points much better than #if/#endif . With #if and #endif blocks, you
can mistakenly remove important method calls or assignments.
The previous examples use the predefined DEBUG or TRACE symbols.
But you can extend this technique for any symbols you define. The Con-
ditional attribute can be controlled by symbols defined in a variety of ways.
Yo u c a n d e fi n e s y m b o l s f r o m t h e c o m p i l e r c o m m a n d l i n e , f r o m e n v i r o n -
ment variables in the operating system shell, or from pragmas in the source
code.
Yo u m a y h a v e n o t i c e d t h a t e v e r y m e t h o d s h o w n w i t h t h e C o n d i t i o n a l
attribute has been a method that has a void return type and takes no
parameters. That's a practice you should follow. The compiler enforces
that conditional methods must have the void return type. However, you
could create a method that takes any number of reference type parameters.
That can lead to practices where an important side effect does not take
place. Consider this snippet of code:
Queue < string > names = new Queue < string >();
names.Enqueue( "one" );
names.Enqueue( "two" );
names.Enqueue( "three" );
 
Search WWH ::




Custom Search