Attributes in C#

Last Updated : 23 Oct, 2025

Attributes in C# are used to add metadata or declarative information to program elements such as classes, methods, properties, fields and assemblies. This metadata can be read by the compiler or retrieved at runtime using reflection to modify behavior or provide additional context.

Attributes are enclosed within square brackets [ ] and placed directly above the element they describe.

Properties of Attributes

  • Attributes can have parameters similar to methods or constructors.
  • A code element can have one or multiple attributes applied.
  • Attributes can be accessed at runtime using reflection.
  • Every attribute in C# is derived from the System.Attribute base class.

Types of Attributes

There are two broad categories of attributes in C#:

  1. Predefined Attributes: Provided by the .NET Framework.
  2. Custom Attributes: Defined by developers for specific use cases.

Predefined Attributes

Predefined attributes are part of the .NET Framework Class Library and are used for common tasks like serialization, version control and interoperation.

  • AttributeUsageAttribute : Defines how a custom attribute can be applied and where
  • CLSCompliantAttribute : Indicates whether a program element complies with the Common Language Specification (CLS)
  • ContextStaticAttribute : Marks a static field as unique per context, not shared
  • FlagsAttribute : Specifies that an enumeration can be treated as a set of bitwise flags
  • LoaderOptimizationAttribute : Suggests the optimization policy for the default loader
  • NonSerializedAttribute : Marks a field in a serializable class to be skipped during serialization
  • ObsoleteAttribute : Marks program elements as obsolete, generating warnings or errors when used
  • SerializableAttribute : Indicates that a class can be serialized
  • ThreadStaticAttribute : Specifies that a static field is unique for each thread
  • DllImportAttribute : Used to call unmanaged functions from a DLL

Examples of Common Attributes

1. CLSCompliantAttribute

Specifies whether code adheres to the Common Language Specification (CLS). CLS compliance ensures that code can be used across all .NET languages.

Example 1: CLS-compliant code 

csharp
using System;

[assembly: CLSCompliant(true)]

public class Demo
{
    public static void Main()
    {
        Console.WriteLine("This code is CLS-compliant");
    }
}

Output
This code is CLS-compliant

Example 2: CLS non-compliant code

csharp
using System;

[assembly: CLSCompliant(true)]

public class Demo
{
    public uint Id; // uint is not CLS-compliant
}

class Program
{
    public static void Main()
    {
        Console.WriteLine("Non-compliant example");
    }
}

Warning:

warning CS3003: Type of 'Demo.Id' is not CLS-compliant

2. FlagsAttribute

Indicates that an enumeration can be treated as a bit field and combined using bitwise operations.

Example: 

csharp
using System;

class Example
{
    enum Colors { Red = 1, Blue = 2, Green = 4 }

    [Flags]
    enum ColorsFlags { Red = 1, Blue = 2, Green = 4 }

    public static void Main()
    {
        Console.WriteLine((Colors.Red | Colors.Blue).ToString());
        Console.WriteLine((ColorsFlags.Red | ColorsFlags.Blue).ToString());
    }
}

Output
3
Red, Blue

Without [Flags], the result is the numeric sum. With [Flags], it displays a combined string representation.

3. ObsoleteAttribute

Marks code elements as deprecated. The compiler can generate a warning or error when obsolete members are used.

Example:

csharp
using System;

class Example
{
    [Obsolete("Use NewMethod instead", true)]
    static void OldMethod()
    {
        Console.WriteLine("Old method");
    }

    static void NewMethod()
    {
        Console.WriteLine("New method");
    }

    public static void Main()
    {
        OldMethod(); // Compiler error
        NewMethod();
    }
}

Error:

error CS0619: 'Example.OldMethod()' is obsolete: 'Use NewMethod instead'

The second argument true tells the compiler to treat usage as an error instead of a warning.

Custom Attributes

Developers can define custom attributes to store additional metadata as per application requirements.

Steps to Create a Custom Attribute

  1. Create a class derived from System.Attribute.
  2. Use [AttributeUsage] to specify how and where it can be applied.
  3. Define constructors and properties for initialization.

Example:

C#
using System;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class InfoAttribute : Attribute
{
    public string Author { get; }
    public string Version { get; }

    public InfoAttribute(string author, string version)
    {
        Author = author;
        Version = version;
    }
}

[Info("Geek", "1.0")]
public class Student
{
    [Info("Geek", "1.1")]
    public void Display()
    {
        Console.WriteLine("Student details displayed");
    }
}
Comment
Article Tags:

Explore