C Sharp Attributes Tutorials
ATTRIBUTES
- It is a tag for declaration
- It is used to pass an information in dynamic way about the behaviours of numerous elements like enumerator, functions, classes, structures, etc.,
- With the help an attribute declarative information can be added in a program
- This tag is represented using square brackets ([])
- It is located on top of the element where it is used
- It is utilized for including metadata
- It includes compiler instruction, comments, explanation, function and classes description, etc.,
- There are 2 kinds of attributes
- Predefined attributes
- Custom Built attributes
Representing an Attribute
Syntax
[attribute (positionalparameters, nameparameter = value, ...)]
Element
Where
positional parameters represents the important data and nameparameter represents the non mandatory data
name and values of an attribute are represented within the square format but previous to the element of an attribute that is applied
There are three predefined attributes. They are
- Attribute usage
- Conditional
- Obsolete
AttributeUsage
It describes the way of custom attribute classNameutilization
Syntax
[attributeusage ( validon, Inherited=inherited, AllowMultiple=allowmultiple)]
where,
- Validon represents elements of a language’s attribute
- Its default value is AttributeTargets.All
- Allowmultiple is not mandatory parameter it gives the multiple field of an attribute
- Its a Boolean value, if true, it is multiuse and false is single use
- Inherited is not mandatory, it gives inheritable option of an attribute
- Its a Boolean value, if true, it is inherited and false is non inherited
Conditional
It makes restricted compilation of calling function
It depends on the given value either DEBUG or TRACE
It shows the values of a variable when error checking occurs
Syntax
[Conditional]( conditionalSymbol)]
Example
[Conditional] (“DEBUG”)]
Obsolete
It highlights not usage
It helps to avoid a exact target element by an already defined attribute
To use new method and to retain old method not function able that is using new one rathe than old one
Syntax
[Obsolete (Message)]
[Obsolete (Message , iserror)]
where
Message is a reason for obsolete and what is next
Iserror is Boolean value, if true item is an error if false warning is generated, its default value
Custom Attributes Creation
It is used to store and retrieve declaring data at a runtime
It involves four process
- Declaration
- Construction
- Applying to the target element
- Accessing
Declaration
The custom attribute is derived fro System.Attribute
[AttributeUsage(AttributeTargets.className| AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false)]
Public classNameExam : System.Attribute
Construction
- Its for storing information obtained by the error free code checking process.
- For example The information are
- Code no.
- Programmer name
- Review due date
- Message
- The first three parameters are private and positional arguments
- and the last one is public and optional or named argument
Example:
![attributes image 1 attributes image 1](https://www.assignmenthelp.net/webimg/ahp/c-sharp-assignment-help/attributes-image-1.jpg)