© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
7 replies
akaJB

✅ How do I deal with generic covariance and collections types?

Let's say I have osmething like the following:

public interface IBoxCollection<out T> : IBox
    where T : IBox
{
    public Dictionary<string, T> Collection { get; }
}
public interface IBoxCollection<out T> : IBox
    where T : IBox
{
    public Dictionary<string, T> Collection { get; }
}


I get the following error:

Invalid variance: The type parameter 'T' must be invariantly valid on 'IBoxCollection<T>.Collection'. 'T' is covariant
Invalid variance: The type parameter 'T' must be invariantly valid on 'IBoxCollection<T>.Collection'. 'T' is covariant


I've looked around on how to resolve this issue but haven't found anything. Is there an easy solution to this problem, or is there a pattern which will accomplish what I'm looking for?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Generic types and inheritance
C#CC# / help
3y ago
✅ How to deal with different JSON types
C#CC# / help
3y ago
How do collections implement Remove and Contains?
C#CC# / help
3y ago
❔ Covariance and Contravariance C#
C#CC# / help
3y ago