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()
        {
        }
    }
}

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...