❔ You cannot use a covariant type parameter as a generic type constraint for interface methods.
I do understand what variant type parameters are after reading about covariance and contravariance. What I do not understand however is what the sentence "You cannot use a covariant type parameter as a generic type constraint for interface methods." or "You can use a contravariant type parameter as a generic type constraint for an interface method." means from https://learn.microsoft.com/en-us/dotnet/standard/generics/covariance-and-contravariance#define-variant-generic-interfaces-and-delegates. Can someone explain to me the meaning of this sentence is?
Covariance and Contravariance in Generics - .NET
Learn about covariance, which allows you to use a more derived type, and contravariance, which allows you to use a less derived type, in .NET generics.
10 Replies
example?
They don't give an example in the documentation. Its just that sentence.
an example of what you are trying to do
I was just reading the documentation on Covariance and Contravariance without trying to do something specifically. Just studying C# details
ah
well
which piece(s) of that sentence are you having trouble with?
Basically I do not understand this sentence as a whole: "You cannot use a covariant type parameter as a generic type constraint for interface methods."
I did ask bard about this and it came up with a term called "covariance hole". But that does seem to be made up because I could not find that anywhere else
covariant type parameter
generic type constraint
interface methodso, I'd have to say it means you can't do this
Oh I see. Thank you for the example!
if you want a better answer, try #advanced or #roslyn
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.