© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
4 replies
knut.wannheden

Generic types API design

Coming from Java I am used to generics having type erasure at runtime and also wildcard types (like e.g.
Foo<?>
Foo<?>
) at design time. In C# the type parameters are reified and take some getting used to.

I am working on a framework where I have types using generics and also API methods which declare parameters of these generic types. But in many cases the methods don't really have to care about the type parameters, which is when I in Java would have declared the method parameter using a wildcard, rather than adding a type parameter to the method signature.

Now I am wondering how this is typically approached in C#. What I ended up doing, but probably isn't what you are supposed to do, is that I declared an interface
IFoo
IFoo
without type parameters and then another one with type parameters as
IFoo<T> : IFoo
IFoo<T> : IFoo
. Now I can declare method parameters of type
IFoo
IFoo
(or
IFoo[]
IFoo[]
or similar) when I don't need to call methods which require the type parameters. I suspect that this is probably more of an anti pattern and I would like to learn how to better design APIs in C#.
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
Struggling with generic class... design?
C#CC# / help
2y ago