C# Error CS0616 – ‘{0}’ is not an attribute class

C# Error

CS0616 – ‘{0}’ is not an attribute class

Reason for the Error & Solution

‘class’ is not an attribute class

An attempt was made to use a non-attribute class in an attribute block. All the attribute types need to be inherited from .

Example 1

The following sample generates CS0616.

// CS0616.cs  
// compile with: /target:library  
[CMyClass(i = 5)]   // CS0616  
public class CMyClass {}  

Example 2

The following sample shows how you might define an attribute:

// CreateAttrib.cs  
// compile with: /target:library  
using System;  
  
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface)]  
public class MyAttr : Attribute  
{  
   public int Name = 0;  
   public int Count = 0;  
  
   public MyAttr (int iCount, int sName)  
   {  
      Count = iCount;  
      Name = sName;  
   }  
}  
  
[MyAttr(5, 50)]  
class Class1 {}  
  
[MyAttr(6, 60)]  
interface Interface1 {}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...