C# Error CS0080 – Constraints are not allowed on non-generic declarations

C# Compiler Error

CS0080 – Constraints are not allowed on non-generic declarations

Reason for the Error

You will receive this error when you have used a syntax on a non-generic where it is only supported on a generic class.

For example, try compiling the below code snippet.

namespace DeveloperPublish
{
    public class Class1 where Class1 : System.IDisposable 
    {

    }

    public class Program
    {
        public static void Main()
        {
        }
    }
}

You will receive the C# compiler error CS0080 as the class Class1 is not a generic class and we are using the keyword “where” when defining the class.

Error CS0080 Constraints are not allowed on non-generic declarations ConsoleApp2 C:\Users\admin\source\repos\ConsoleApp2\ConsoleApp2\Program.cs 3 Active

C# Error CS0080 – Constraints are not allowed on non-generic declarations

Solution

To fix the error in the above code, ensure that you use the generic implementation as shown below

namespace DeveloperPublish
{
    public class Class1<T> where T : System.IDisposable 
    {

    }

    public class Program
    {
        public static void Main()
        {
        }
    }
}
Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Hey, wait!Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.