Compiler Error CS0592
TOC
Collapse the table of content
Expand the table of content

Compiler Error CS0592

Attribute 'attribute' is not valid on this declaration type. It is valid on 'type' declarations only.

When you define an attribute, you define what constructs it can be applied to by specifying an AttributeTargets value. In the following example, the MyAttribute attribute can be applied to interfaces only, because the AttributeUsage attribute specifies AttributeTargets.Iterface. The error is generated because the attribute is applied to a class (class A).

The following sample generates CS0592:

// CS0592.cs
using System;

[AttributeUsage(AttributeTargets.Interface)]
public class MyAttribute : Attribute 
{
}

[MyAttribute]
// Generates CS0592 because MyAttribute is not valid for a class. 
public class A  
{
    public static void Main()
    {
    }
}
Show:
© 2016 Microsoft