Skip to main content

How to Resolve Error "CS0616: 'class' is not an attribute class" in C#

The Compiler Error CS0616 is a type usage error related to Attributes. The message reads: "'MyClass' is not an attribute class".

In C#, Attributes (metadata tags like [Serializable], [Obsolete], or [Test]) are specialized classes. You cannot use just any class as an attribute. To be used inside square brackets [...] to decorate a method, class, or property, a class must inherit from the base class System.Attribute. If the class does not have this inheritance relationship, the compiler raises CS0616.

This guide explains how to define custom attributes correctly.

Understanding Attribute Requirements

Attributes in .NET are metadata. When you write [MyAttribute], the compiler actually instantiates the class MyAttribute.

For this mechanism to work, the .NET runtime enforces a rule: All Attribute classes must derive from System.Attribute (directly or indirectly). This base class provides the necessary machinery for reflection and metadata storage.

Scenario: Using a Standard Class as an Attribute

This error typically occurs when a developer creates a class intending it to be a marker or metadata container but forgets the inheritance step.

Example of error: defining a plain class and trying to apply it to a method.

// Just a regular class. No inheritance.
public class MyCustomInfo
{
public string Description { get; set; }
}

public class Program
{
// ⛔️ Error CS0616: 'MyCustomInfo' is not an attribute class.
// The compiler sees it does not inherit from System.Attribute.
[MyCustomInfo]
public void DoWork()
{
}
}

Solution: Inherit from System.Attribute

To turn a standard class into a valid Attribute, simply add : Attribute (or System.Attribute) to the class definition.

Solution:

using System;

// 1. Inherit from Attribute
// 2. Convention: End the name with "Attribute" (e.g., MyCustomInfoAttribute)
public class MyCustomInfoAttribute : Attribute
{
public string Description { get; set; }
}

public class Program
{
// ✅ Correct: The compiler recognizes this as a valid attribute.
// Note: You can omit the "Attribute" suffix when using it.
[MyCustomInfo]
public void DoWork()
{
}
}
note

Naming Convention: While C# allows you to name the class anything (e.g., MyTag), standard convention dictates appending the suffix Attribute (e.g., MyTagAttribute). The compiler allows you to use [MyTag] as shorthand for [MyTagAttribute].

Conclusion

CS0616 is a simple inheritance check.

  1. Check the Class: Look at the definition of the class you are trying to use in brackets.
  2. Add Inheritance: Ensure it is defined as public class MyClass : System.Attribute.