> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C# Attribute

An attribute in C# is a declarative tag used to associate metadata with program elements such as assemblies, types, methods, properties, and fields. During compilation, the C# compiler bakes this metadata into the assembly's metadata tables, which are distinct from the Common Intermediate Language (CIL) instruction stream. This embedded metadata does not directly affect the execution of the code but can be inspected and acted upon at runtime using Reflection.

## Syntax

Attributes are applied by enclosing the attribute name and its arguments in square brackets `[]` immediately above the target element.

```csharp theme={"dark"}
[AttributeName(positionalArgument, NamedArgument = value)]
public class TargetElement 
{
    [AttributeName]
    public void TargetMethod() { }
}
```

By convention, all attribute class names end with the word `Attribute`. The C# compiler allows you to omit this suffix when applying the attribute in code (e.g., `[Obsolete]` instead of `[ObsoleteAttribute]`).

## Anatomy of a Custom Attribute

Under the hood, an attribute is simply a class that inherits directly or indirectly from the `System.Attribute` base class.

```csharp theme={"dark"}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public sealed class MetadataAttribute : System.Attribute
{
    // Positional parameter (Constructor argument)
    public MetadataAttribute(string author)
    {
        Author = author;
    }

    public string Author { get; }

    // Named parameter (Public read-write property)
    public string Version { get; set; }
}
```

### Parameter Types

Attribute parameters are strictly limited by the compiler. They can only be:

* Simple types (`bool`, `byte`, `sbyte`, `char`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `float`, `double`)
* `string`
* `System.Type`
* Enums
* `object` (though the actual argument passed at compile-time must evaluate to one of the other permitted types)
* Single-dimensional arrays of any of the above

### Positional vs. Named Parameters

When applying an attribute, arguments are passed based on the attribute class's design:

1. **Positional Parameters:** These map directly to the constructor signatures of the attribute class. They are mandatory and must be specified in the exact order defined by the constructor.
2. **Named Parameters:** These map to public, non-static, read-write fields or properties on the attribute class. They are optional and are specified using the `PropertyName = value` syntax after all positional parameters.

```csharp theme={"dark"}
// "Alice" is positional; Version is named.
[Metadata("Alice", Version = "1.0.0")] 
public class Processor { }
```

## The `[AttributeUsage]` Meta-Attribute

When defining a custom attribute, its behavior and valid application targets are governed by applying the `System.AttributeUsageAttribute` to the custom attribute class itself. It accepts three configurations:

1. **ValidOn (`AttributeTargets`):** A bitwise combination of `AttributeTargets` enum values (e.g., `Class`, `Method`, `Property`, `All`) dictating exactly which code elements the compiler will allow the attribute to be attached to.
2. **AllowMultiple (`bool`):** Determines whether the same attribute can be applied more than once to a single program element.
3. **Inherited (`bool`):** Determines whether the attribute is automatically inherited by derived classes or overriding methods.

## Metadata Retrieval

Because attributes are compiled into the assembly's metadata tables, they remain dormant until explicitly queried. The .NET runtime provides Reflection APIs, primarily through the `System.Reflection.MemberInfo.GetCustomAttributes` method, to instantiate and read the attribute objects at runtime.

```csharp theme={"dark"}
Type targetType = typeof(Processor);

// Extracts the attribute metadata from the compiled assembly
object[] attributes = targetType.GetCustomAttributes(typeof(MetadataAttribute), false);

foreach (MetadataAttribute attr in attributes)
{
    Console.WriteLine($"Author: {attr.Author}, Version: {attr.Version}");
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
