Share via


Compiler Error CS0451

The 'new()' constraint cannot be used with the 'struct' constraint

When specifying constraints on the type of a generic, the new() constraint may only be used with class type constraints, interface type constraints, reference type constraints, and type parameter constraints, but not with value type constraints.

Example

The following sample generates CS0451.

// CS0451.cs
using System;
public class C4 
{
   public void F4<T>() where T : struct, new() {}   // CS0451
}

// OK
public class C5
{
   public void F5<T>() where T : struct {}
}

public class C6
{
   public void F6<T>() where T : new() {}
}