---
title: "C# Attributes – A Beginner's Guide"  
description: "They are a powerful way to annotate classes, methods, properties, and other program elements with additional information"  
author: "Anubhav Sharma"  
published: 2025-04-22  
updated: 2025-04-22  
canonical: https://www.mindstick.com/blog/305503/c-sharp-attributes-a-beginner-s-guide  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# C# Attributes – A Beginner's Guide

In C#, **attributes** add **metadata** (data about data) to [your code](https://www.mindstick.com/forum/157976/how-can-you-use-jquery-s-testing-framework-such-as-qunit-to-write-unit-tests-for-your-code). They are a powerful way to **annotate** classes, methods, properties, and other program elements with [additional information](https://www.mindstick.com/forum/160484/error-failed-to-launch-debug-adapter-additional-information-may-be-available-in-the-output-window) that can be used by the **compiler**, **runtime**, or **tools**.

### What Are Attributes?

Attributes provide descriptive information about your code. For example, you can mark a [method as](https://www.mindstick.com/forum/159054/how-to-mark-a-method-as-obsolete-or-deprecated-in-c-sharp) *obsolete*, specify how a property should be serialized, or define custom metadata for your own tools.

### Real-Life Analogy:

Think of attributes as sticky notes on your code — they don’t change the code's logic directly but provide useful instructions.

## Basic Syntax

```cs
[AttributeName]
```

## With parameters

```cs
[AttributeName(param1, param2)]
```

### Commonly Used Attributes in C#

| Attribute | Description |
| --- | --- |
| `[Obsolete]` | Marks code as outdated or deprecated |
| `[Serializable]` | Allows an object to be serialized |
| `[NonSerialized]` | Prevents a field from being serialized |
| `[DllImport]` | Used for interoperability with [native DLLs](https://www.mindstick.com/forum/23025/accessing-c-sharp-native-dlls-in-matlab) |
| `[DebuggerStepThrough]` | Skips method when debugging |
| `[Required]` | Used in validation (usually with MVC / [Data Annotations](https://www.mindstick.com/forum/160240/purpose-of-asp-dot-net-core-data-annotations)) |
| `[Display(Name="")]` | Sets a friendly name for a property |

**Example 1:** `[Obsolete]`

```cs
[Obsolete("Use NewMethod() instead")]
public void OldMethod()
{
    Console.WriteLine("This method is obsolete.");
}
```

Calling `OldMethod()` will show a [compiler warning](https://www.mindstick.com/forum/877/simplest-an-fastest-c-sharp-expression-that-always-evaluates-to-false-and-does-not-generate-a-compiler-warning).

**Example 2:** `[Serializable]`

```cs
[Serializable]
public class Person
{
    public string Name;
    public int Age;
}
```

**Example 3: Data [Annotation Attributes](https://www.mindstick.com/forum/34680/how-many-types-of-data-annotation-attributes-in-mvc) (in [ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc))**

```cs
public class User
{
    [Required]
    [Display(Name = "Full Name")]
    public string Name { get; set; }

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

1. `[Required]` makes sure `Name` is not null or empty.
2. `[Display]` changes the label name.
3. `[Range]` sets valid value boundaries.

### Creating Custom Attributes

You can define your own attributes by extending `System.Attribute`.

```cs
public class AuthorAttribute : Attribute
{
    public string Name;
    public AuthorAttribute(string name)
    {
        Name = name;
    }
}
```

## Use it like:

```cs
[Author("John Doe")]
public class SampleClass
{
}
```

### Reading Attributes via Reflection

Attributes can be accessed at runtime using **reflection**.

```cs
Type type = typeof(SampleClass);
object[] attrs = type.GetCustomAttributes(false);

foreach (var attr in attrs)
{
    Console.WriteLine(attr);
}
```

## Summary

| Feature | Description |
| --- | --- |
| What is it? | Metadata you attach to your code |
| Usage | With square brackets above code elements |
| Purpose | Inform compiler, tools, or at runtime |
| Custom? | Yes, you can create your own attributes |
| Reflection | Used to read attribute data at runtime |

Attributes make your code **more informative, maintainable, and flexible**. From marking code as obsolete to controlling serialization, they play a crucial role in many frameworks — especially in **ASP.NET**, **[Entity Framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach)**, and **Xamarin**.

---

Original Source: https://www.mindstick.com/blog/305503/c-sharp-attributes-a-beginner-s-guide

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
