Home > Mobile >  If I have a generic class/ interface, is it good practice to define a non-generic base class/interfa
If I have a generic class/ interface, is it good practice to define a non-generic base class/interfa

Time:06-22

I have a generic interface and I have no functional need for a non-generic version. However, all of the CLR and professional code I've seen has defined a non-generic base interface. Is this for a functional reason, a guideline, or just community habit?

CodePudding user response:

No, if there is no functional need for it, don't add it.

There is some legacy in the framework classes due to historic reasons. But if you take a look at some newer interfaces like IReadOnlyCollection it only have a generic version. Older interfaces like IEnumerable predates the generic version.

There are some situations where a non generic base type is useful, like when you want to treat a bunch of objects with different generic parameters in the same way, but that is a rather specialized use case.

  • Related