What Are Attributes in .NET?
Ask by ICSM Computer
Updated 12 Feb 2026
In .NET, attributes are special classes used to add metadata (extra information) to your code elements such as:
They provide additional information that can be read at compile time or runtime using reflection.
Simple Definition
An attribute in .NET is a declarative tag that you attach to code elements to describe behavior or configuration.
Why Are Attributes Used?
Attributes are used to:
Basic Syntax
Attributes are written inside square brackets
[ ].Here,
[Obsolete]is a built-in attribute.Example 1: Built-in Attribute
1. Obsolete Attribute
When someone calls this method, the compiler gives a warning.
Example 2: Validation Attributes (ASP.NET MVC)
These attributes are used by model validation in ASP.NET MVC.
Example 3: Custom Attribute
You can create your own attribute.
Step 1: Create Custom Attribute Class
Step 2: Use Custom Attribute
AttributeUsage
The
AttributeUsageattribute defines where your custom attribute can be applied.Common targets:
Reading Attributes Using Reflection
Attributes can be read at runtime using reflection.
Types of Attributes in .NET
1. Predefined Attributes
Examples:
2. Validation Attributes
Used in ASP.NET MVC:
3. Custom Attributes
Developer-defined attributes for:
Real-World Use Cases (Especially for You – ASP.NET MVC)
Since you work with ASP.NET MVC 5, attributes are commonly used for:
Authorization
Custom Authorization Attribute