-
The following code should compile without error: F(new Nullable<int>(1)); // error CS8377
void F<T>(T value) where T : unmanaged
{
} Currently the compiler reports an error:
F(new MyNullable<int>(1));
public struct MyNullable<T> where T : struct
{
private readonly bool hasValue;
internal T value;
public MyNullable(T value)
{
this.value = value;
hasValue = true;
}
} The following also compiles without error: F(new S());
void F<T>(T value) where T : unmanaged
{
}
struct S
{
int? a;
} |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
Beta Was this translation helpful? Give feedback.
-
Filed dotnet/runtime#81177 to track ILVerify reporting error if this was allowed by the compiler. |
Beta Was this translation helpful? Give feedback.
-
Not only unmanaged but also struct constraints: static void m1<T>() where T : struct { }
static void m2<T>() where T : unmanaged { }
m1<int>();
m1<int?>(); // error CS8377
m1<(int?, int?)>(); // no error
m1<Wrap<int?>>(); // no error
m2<int>();
m2<int?>(); // error CS8377
m2<(int?, int?)>(); // no error
m2<Wrap<int?>>(); // no error
struct Wrap<T> { public T Value; } |
Beta Was this translation helpful? Give feedback.
-
Closing based on discussion in dotnet/runtime#81177 |
Beta Was this translation helpful? Give feedback.
-
I'm not very convinced by the reasoning in dotnet/runtime#81177. Specifically, if the requirement is to prevent |
Beta Was this translation helpful? Give feedback.
Closing based on discussion in dotnet/runtime#81177