The Conditional Attribute

Problem
You want to exclude a method and any statements that call it from a particular version of your application. How should you do it?

Solution
Prefer the Conditional attribute instead of the #if directive to exclude methods and all the statements that call them.

Comments
The Conditional attribute can abandon all statements that call a method, but it won’t discard the method definition itself. However, you can invoke the method via reflection.

Example

// This is incorrect.
#if BETAVERSION
    ShowSplashScreen();
#endif

void ShowSplashScreen() {
    ...
}

// This is correct. No need for an #if directive.
ShowSplashScreen();

[Conditional("BETAVERSION")]
void ShowSplashScreen() {
    ...
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s