Wednesday, March 24, 2010

Attribute Parameters

Consider this example, which indicates a deprecated method by applying the ObsoleteAttribute attribute:
[Obsolete]
public static void MyMethod()
{
    //The body of the obsolete method.
}
When MyMethod is executed, we get the warning:
‘MyClass.MyMethod()’ is obsolete.
Some attributes such as Obsolete allow the programmer to provide extra information on using the deprecated method. We can do this by using the following modified version, which includes two parameters:
[System.Obsolete(“Use MyNewMethod instead.”, false)]
public static void MyMethod()
{
    //The body of the obsolete method.
}
When we compile this method, the compiler generates the warning:
‘MyClass.MyMethod()’ is obsolete: ‘Use MyNewMethod instead.’
The first parameter contains the string message that we would like to add. The second parameter causes the compilation to generate either an error or a warning, depending on whether it is false or true. The default value is false.

No comments:

Post a Comment