C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Built-in Attributes : AttributeUsage, Conditional, and Obsolete


Posted Date: 04 Apr 2004    Resource Type: Articles    Category: .NET Framework
Author: ANITA MARY JOSEPHMember Level: Gold    
Rating: 1 out of 5Points: 7



AttributeUsage
The AttributeUsage attribute mentions the types of items to which an attribute can be applied. AttributeUsage is nothing but the System.AttributeUsageAttribute class.

The Syntax below shows the constructor of AttributeUsage:

AttributeUsage(AttributeTargets item)

Here,item tells us about the item or items upon which the attribute can be used. AttributeTargets is an enumeration and it defines the following values:

All Assembly Class Constructor
Delegate Enum Event Field
Interface Method Module Parameter
Property Return Value Struct

We can OR together two or more of these values. Example:

AttributeTargets.Interface | AttributeTargets.Method




Conditional Attribute
A conditional method is invoked only if a specific symbol has been defined via #define. else the method is bypassed. To use the Conditional attribute, we should include the System.Diagnostics namespace.

Example:-

//Demonstrate the Conditional attribute.

#define ONE


using System;

using System.Diagnostics;

class Demo

{

[Conditional( "ONE" )]

void one()

{

Console.WriteLine( "ONE IS DEFINED" );

}



[Conditional("TWO")]

void two()

{

Console.WriteLine( "TWO IS NOT DEFINED" );

}

public static void Main()

{

Demo example = new Demo();

example.one(); // will be called only if ONE is defined.

example.two(); // will be called only if TWO is defined

Console.Read();

}

}

Inside the Main(), We call both one() and two() . But, only one() is executed since ONE alone was defined. Since TWO was not defined the call to two() is ignored. Incase you want to call two() make sure you define it before hand.


Obsolete Attribute
With an Obsolete attribute, you can mark a program element as obsolete.
Here’s the general form:

[Obsolete(“data”)]

According to the syntax, data is displayed when that program element is compiled.
Example:-

//Demonstrate the Obsolete attribute.

using System;

class Demo

{

[Obsolete( "Please use method2." )]

static int method1( int a ,int b )

{

return a * b;

}

//Improved version of method1.

static int method2( int a , int b )

{

return a + a;

}

public static void Main()

{

//A warning will be displayed here

Console.WriteLine( " 3 * 2 is " +Demo.method1(3,2));



//no warning in this case.

Console.WriteLine( " 3 + 3 is " +Demo.method2(3,2));

Console.Read();

}

}

When the program is compiled a call to method1() will generate a warning that tells the user to use method2() instead.

Here’s an other form of Obsolete:

[Obsolete( “data”,error )]

Here, error is a Boolean value. The obsolete item generates a compilation error rather than a warning when the error value is true.



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Attributes : The Additional Information that is associated with a class, structure, method...
Previous Resource: Primitive data types
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use