-
Example code: #nullable enable
using System.Collections.Generic;
public interface I1 {
IEnumerable<T?> M<T>();
}
public class C1 : I1 {
public IEnumerable<T?> M<T>() => new T[0];
}
public interface I2 {
IEnumerable<T?> M<T>();
}
public class C2 : I2 {
// CS0453: The type 'T' must be a non-nullable value type in order
// to use it as parameter 'T' in the generic type or method 'Nullable<T>'
// (and few other errors, but this is the main one I think)
IEnumerable<T?> I2.M<T>() => new T[0];
} Why does |
Beta Was this translation helpful? Give feedback.
Answered by
333fred
Apr 10, 2021
Replies: 1 comment 2 replies
-
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
ashmind
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is one of the cases you need a
where T : default
: https://sharplab.io/#v2:D4YgdgrgNlCGBGUCmACJYHILAChcAEAGFfARgBYBuXAgZhQEswAXJAJwDNYBjVASVIoA3rhRiSpWgB4AKgH4AfCgCyshQAoAlNRwBfOiQBMKAMKCAXCgHDR4/PTLT5S1TI2aUAXiVgkAdxQZAG1CAF0dfTwce0YWdi5eK2MRHHEJJ0UVNS0Ig3xjE2NLPmTbMUdZTJKAOld3FD8AC3ZUGRRLABMkLmhmbxRfAOCwiKA=