Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C#
Article

Conditional Methods using Conditional Attribute

Rate me:
Please Sign up or sign in to vote.
2.00/5 (10 votes)
18 Jan 20062 min read 30K   9   1
Conditional Methods using Conditional Attribute

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.

Example:

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></FONT></FONT>
<BLOCKQUOTE>
<FONT face=Arial color=#0000ff size=2>[Conditional("CON_Attrib1")]</FONT>

<FONT face=Arial color=#0000ff size=2>public void MyFun1(string name)</FONT>

<FONT face=Arial color=#0000ff size=2>{</FONT>

<FONT face=Arial color=#0000ff size=2>    Console.WriteLine(name);</FONT>

<FONT face=Arial color=#0000ff size=2>}</FONT>

<FONT face=Arial color=#0000ff size=2>[Conditional("CON_Attrib1"),Conditional("CON_Attrib2")]</FONT>

<FONT face=Arial color=#0000ff size=2>public virtual void MyFun2(string name)</FONT>

<FONT face=Arial color=#0000ff size=2>{</FONT>

<FONT face=Arial color=#0000ff size=2>Console.WriteLine(name);</FONT>

<FONT face=Arial color=#0000ff size=2>}</FONT>
</BLOCKQUOTE><P>
<FONT face=Arial color=#0000ff size=2>}</FONT></P>
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></FONT></FONT>
<BLOCKQUOTE><FONT face=Arial color=#0000ff size=2>public MyClient()</FONT>

<FONT face=Arial color=#0000ff size=2>{</FONT><FONT face=Arial color=#0000ff size=2>}</FONT>

<FONT face=Arial color=#0000ff size=2>public static void Main() </FONT>

<FONT face=Arial color=#0000ff size=2>{</FONT>
 
<FONT face=Arial color=#0000ff size=2>   MultiFile1.MyClass oMyClass = new MyClass();</FONT> 
<FONT face=Arial color=#0000ff size=2>   oMyClass.MyFun1("Namratha");</FONT>  
<FONT face=Arial color=#0000ff size=2>   oMyClass.MyFun2("Nasha");</FONT>  
<FONT face=Arial color=#0000ff size=2>   Console.ReadLine();</FONT>

<FONT face=Arial color=#0000ff size=2>}</FONT>
</BLOCKQUOTE><P>
<FONT face=Arial color=#0000ff size=2>}</FONT></P>

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 .....

License

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


Written By
Architect
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.

Comments and Discussions

 
QuestionControlling #DEFINE values from an external property file Pin
Member 471344427-Oct-08 23:07
Member 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.