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() {}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for