What are the attributes in C#?


An attribute is a declarative tag that is used to convey information to runtime about the behaviours of various elements like classes, methods, structures, enumerators, assemblies etc. in your program.

The following is the syntax.

[attribute(positional_parameters, name_parameter = value, ...)]
Element

Here,

Name of the attribute and its values are specified within the square brackets, before the element to which the attribute is applied.

Positional parameters specify the essential information and the name parameters specify the optional information.

The following are the predefined attributes in C#.

AttributeUsage

The pre-defined attribute AttributeUsage describes how a custom attribute class can be used.

Conditional

This predefined attribute marks a conditional method whose execution depends on a specified preprocessing identifier.

Obsolete

Th Obsolete predefined attribute marks a program entity that should not be used. It enables you to inform the compiler to discard a particular target element

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements