---
title: "What Are Attributes in .NET?"  
description: "What Are Attributes in .NET?"  
author: "Ravi Vishwakarma"  
published: 2026-02-12  
updated: 2026-02-12  
canonical: https://www.mindstick.com/interview/34453/what-are-attributes-in-dot-net  
category: "asp.net"  
tags: [".net", "asp.net", ".net core"]  
reading_time: 5 minutes  

---

# What Are Attributes in .NET?

In **.NET**, attributes are special classes used to add **metadata** (extra information) to your code elements such as:

- Classes
- Methods
- Properties
- Fields
- Parameters
- Assemblies

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:

- Control behavior of frameworks (ASP.NET MVC, Web API)
- Define validation rules
- Configure serialization
- Define authorization rules
- Mark obsolete methods
- Provide metadata for tools and libraries

## Basic Syntax

Attributes are written inside square brackets `[ ]`.

```plaintext
[Obsolete("Use NewMethod instead")]
public void OldMethod()
{
}
```

Here, `[Obsolete]` is a built-in attribute.

## Example 1: Built-in Attribute

### 1. Obsolete Attribute

```plaintext
[Obsolete("This method is deprecated.")]
public void OldMethod()
{
}
```

When someone calls this method, the compiler gives a warning.

## Example 2: Validation Attributes (ASP.NET MVC)

```plaintext
public class UserModel
{
    [Required]
    [StringLength(100)]
    public string Name { get; set; }

    [Range(18, 60)]
    public int Age { get; set; }
}
```

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

```plaintext
using System;

[AttributeUsage(AttributeTargets.Method)]
public class LogAttribute : Attribute
{
    public string Message { get; }

    public LogAttribute(string message)
    {
        Message = message;
    }
}
```

### Step 2: Use Custom Attribute

```plaintext
[Log("This method performs calculation")]
public void Calculate()
{
}
```

## AttributeUsage

The `AttributeUsage` attribute defines where your custom attribute can be applied.

```plaintext
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
```

Common targets:

- Class
- Method
- Property
- Field
- Parameter
- Assembly

## Reading Attributes Using Reflection

Attributes can be read at runtime using reflection.

```plaintext
var method = typeof(MyClass).GetMethod("Calculate");
var attribute = (LogAttribute)Attribute.GetCustomAttribute(method, typeof(LogAttribute));

if (attribute != null)
{
    Console.WriteLine(attribute.Message);
}
```

## Types of Attributes in .NET

## 1. Predefined Attributes

Examples:

- Obsolete
- Serializable
- Conditional
- AttributeUsage

## 2. Validation Attributes

Used in ASP.NET MVC:

- Required
- Range
- StringLength
- Compare
- EmailAddress

## 3. Custom Attributes

Developer-defined attributes for:

- Logging
- Auditing
- Permissions
- Mapping
- Metadata handling

## Real-World Use Cases (Especially for You – ASP.NET MVC)

Since you work with ASP.NET MVC 5, attributes are commonly used for:

### Authorization

```plaintext
[Authorize]
public ActionResult Dashboard()
{
    return View();
}
```

### Custom Authorization Attribute

```plaintext
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        return httpContext.User.Identity.IsAuthenticated;
    }
}
```

###

## Answers

### Answer by Ravi Vishwakarma

In **.NET**, attributes are special classes used to add **metadata** (extra information) to your code elements such as:

- Classes
- Methods
- Properties
- Fields
- Parameters
- Assemblies

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:

- Control behavior of frameworks (ASP.NET MVC, Web API)
- Define validation rules
- Configure serialization
- Define authorization rules
- Mark obsolete methods
- Provide metadata for tools and libraries

## Basic Syntax

Attributes are written inside square brackets `[ ]`.

```plaintext
[Obsolete("Use NewMethod instead")]
public void OldMethod()
{
}
```

Here, `[Obsolete]` is a built-in attribute.

## Example 1: Built-in Attribute

### 1. Obsolete Attribute

```plaintext
[Obsolete("This method is deprecated.")]
public void OldMethod()
{
}
```

When someone calls this method, the compiler gives a warning.

## Example 2: Validation Attributes (ASP.NET MVC)

```plaintext
public class UserModel
{
    [Required]
    [StringLength(100)]
    public string Name { get; set; }

    [Range(18, 60)]
    public int Age { get; set; }
}
```

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

```plaintext
using System;

[AttributeUsage(AttributeTargets.Method)]
public class LogAttribute : Attribute
{
    public string Message { get; }

    public LogAttribute(string message)
    {
        Message = message;
    }
}
```

### Step 2: Use Custom Attribute

```plaintext
[Log("This method performs calculation")]
public void Calculate()
{
}
```

## AttributeUsage

The `AttributeUsage` attribute defines where your custom attribute can be applied.

```plaintext
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
```

Common targets:

- Class
- Method
- Property
- Field
- Parameter
- Assembly

## Reading Attributes Using Reflection

Attributes can be read at runtime using reflection.

```plaintext
var method = typeof(MyClass).GetMethod("Calculate");
var attribute = (LogAttribute)Attribute.GetCustomAttribute(method, typeof(LogAttribute));

if (attribute != null)
{
    Console.WriteLine(attribute.Message);
}
```

## Types of Attributes in .NET

## 1. Predefined Attributes

Examples:

- Obsolete
- Serializable
- Conditional
- AttributeUsage

## 2. Validation Attributes

Used in ASP.NET MVC:

- Required
- Range
- StringLength
- Compare
- EmailAddress

## 3. Custom Attributes

Developer-defined attributes for:

- Logging
- Auditing
- Permissions
- Mapping
- Metadata handling

## Real-World Use Cases (Especially for You – ASP.NET MVC)

Since you work with ASP.NET MVC 5, attributes are commonly used for:

### Authorization

```plaintext
[Authorize]
public ActionResult Dashboard()
{
    return View();
}
```

### Custom Authorization Attribute

```plaintext
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        return httpContext.User.Identity.IsAuthenticated;
    }
}
```

###


---

Original Source: https://www.mindstick.com/interview/34453/what-are-attributes-in-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
