Attributes are just meta-data associated to the code.
You can create your own:
- Create a child class of
System.Attribute
- The name of your attribute class should end with Attribute (convention sufix)
- Omit the subfix when actually using the attribute.
- (optional) Use the
AttributeUsage
attribute to specify to which constructions the attribute may apply to (Class, Interface, Methods, Constructors...)
Example:
[AttributeUsageAttribute(AttributeTargets.All, Inherited = true, AllowMultiple = false)]
public MyOwnAttribute : System.Attribute
{
}
...
[MyOwn]
public class MyClass
{
[MyOnw]
public MyClass()
{
}
}
Marks a class / method / interface as obsolete. The compiler will throw a warning each time we reference the construct.
[Obsolete]
int DeprecatedMethod()
{
}
Although it is optional, it is highly recommended that you leave a meaningful warning message:
[Obsolete("To be deprecated in v1.1. Use NewMethod instead")]
int DeprecatedMethod()
{
}
Compiles a method (or class) iff a preprocessor variable is set.
// The conditional method MUST NOT return any value (void)
[Conditional("DEBUG")]
void ConditionalMethod()
{
}
The above code is equivalent to:
#if DEBUG
void ConditionalMethod()
{
}
#endif