Attributes in C#
Attributes are used in C# to convey declarative information or metadata about various code elements such as methods, assemblies, properties, types, etc. Attributes are added to the code by using a declarative tag that is placed using square brackets ([ ]) on top of the required code element. There are two types of Attributes implementations provided by the .NET Framework are:
Properties of Attributes
- Attributes can have arguments just like methods, properties, etc. can have arguments.
- Attributes can have zero or more parameters.
- Different code elements such as methods, assemblies, properties, types, etc. can have one or multiple attributes.
- Reflection can be used to obtain the metadata of the program by accessing the attributes at run-time.
- Attributes are generally derived from the System. Attribute Class.
Predefined attributes are those attributes that are a part of the .NET Framework Class Library and are supported by the C# compiler for a specific purpose. Some of the predefined attributes are derived from the System. Attribute base classes are given as follows:
|AttributeUsageAttribute||This attribute specifies the usage of a different attribute.|
|CLSCompliantAttribute||This attribute shows if a particular code element complies with the Common Language Specification.|
|ContextStaticAttribute||This attribute designates that a static field should not be shared between contexts.|
|FlagsAttribute||The FlagsAttribute specifies that an enumeration can be used as a set of flags. This is most commonly used with bitwise operators.|
|LoaderOptimizationAttribute||This attribute sets the optimization policy for the default loader in the main method.|
|NonSerializedAttribute||This attribute signifies that the field of the serializable class should not be serialized.|
|ObsoleteAttribute||This attribute marks the code elements that are obsolete i.e. not in use anymore.|
|SerializableAttribute||This attribute signifies that the field of the serializable class can be serialized.|
|ThreadStaticAttribute||This attribute indicates that there is a unique static field value for each thread.|
|DllImportAttribute||This attribute indicates that the method is a static entry point as shown by the unmanaged DLL.|
Let’s discuss some of the predefined attributes:
This attribute shows if a particular code element complies with the Common Language Specification. If a particular code element complies with the Common Language Specification. If it doesn’t, then a warning message is issued by the compiler.
Example 1: Here, it will not give any warning message, and the code compiles successfully.
Example 2: This code will give a warning message by the compiler.
prog.cs(9,14): warning CS3003: Type of `GFG.z’ is not CLS-compliant
The FlagsAttribute specifies that an enumeration can be used as a set of flags. This is most commonly used with bitwise operators.
The ObsoleteAttribute marks the code elements that are obsolete i.e. not in use anymore. Calling these obsolete code elements results in a compiler error.
prog.cs(18,3): error CS0619: `GFG.method1()’ is obsolete: `method1 is obsolete’
Custom attributes can be created in C# for attaching declarative information to methods, assemblies, properties, types, etc. in any way required. This increases the extensibility of the .NET framework. Steps for creating Custom Attributes:
- Define a custom attribute class that is derived from System. Attribute class.
- The custom attribute class name should have the suffix Attribute.
- Use the attribute AttributeUsage to specify the usage of the custom attribute class created.
- Create the constructor and the accessible properties of the custom attribute class.
Please Login to comment...