Click here to Skip to main content
12,547,501 members (55,529 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked

Conditional Methods using Conditional Attribute

, 18 Jan 2006
Rate this:
Please Sign up or sign in to vote.
Conditional Methods using Conditional Attribute
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

Conditional Methods using Conditional Attribute

Conditional Attribute is defined in System.Diagnostics namespace.

When a conditional method is called its call is determined by the presence or absence of the preprocessor symbol used with the conditional attribute. If this preprocessor symbol is defined then the mehotd call is included else omitted.

Using conditional attibute is an alternative to calling methods in #if & #endif preprocessors.

A method can have multiple conditional attributes. The call to such a method is made if at least one of the conditional symbols is defined.

A conditional method has a few constraints:

  • A conditional method must be a method in a class or struct declaration and should not have any return type ... i.e. the return type should be "void".
  • The conditional method must be a method in a class or struct declaration a compile-time error occurs if the Conditional attribute is specified on a method in an interface declaration.
  • Conditional methods cannot be marked with override keyword.
  • Conditional methods can be virtual and the methods which override such a conditional method implicitly becomes Conditional
  • Methods defined in an interface cannot be marked as conditional.


Steps :

1. Create a new Console Application and define 2 classes MyClass and MyClient.

2. In MyClass define two as shown below.

<FONT color=#0000ff><FONT face=Arial>class MyClass

<FONT size=2>{</FONT>
3. In MyClient write the preprocessor directives as shown . (They should be the first two lines in the file... above using System ... if not it will not compile)

#define CON_Attrib1

#define CON_Attrib2

4. Call the My Class methods from the main as shown

<FONT color=#0000ff><FONT face=Arial>public class MyClient

<FONT size=2>{</FONT>

4. Build the solution and run . You will see both "Namratha" & "Nasha" ... i.e both the functions were called.

5. Now change the #define CON_Attrib1 to #undef CON_Attrib1. ... i.e. undefining the attribute.

6. Build the solution and run . You will see only "Nasha" ... i.e MyFun1 was not called as the preprocosser directive "CON_Attrib1" is not defined. But MyFun2 is called coz the other preprocessor directive "CON_Attrib2" is defined. When there are moer than one directives it an OR condition hence if any of the preprocessor directive is define the function will be called.

7. MyFun2 is marked as virtual hence if another function overrides its then is becomes conditional implicitly.

Let's keep things Simple .....


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Namratha Shah
United States United States
Namratha Shah a.k.a. Nasha is from orginally from Bombay, India but currently residing NJ, USA. She has to her credit, a Bachelor’s Degree in Microbiology and Biotechnology and a Master's in Computer and Software Applications (1999-2001) from Somaiya College Bombay. She started her career with C and C++ and then moved on to Microsoft Technologies. She has over 7.5 years experience in software architecture, design and development. She is a Certified Scrum Master and a member of the CORE .NET Architecture team. She has been Awarded with Microsoft’s Prestigious Most Valuable Professional (MVP) twice consecutively in years 2005 and 2006 in Visual C#.NET for her outstanding contributions to the .NET community.

You may also be interested in...


Comments and Discussions

QuestionControlling #DEFINE values from an external property file Pin
Member 471344427-Oct-08 23:07
memberMember 471344427-Oct-08 23:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161018.1 | Last Updated 18 Jan 2006
Article Copyright 2006 by Namratha Shah
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid