c# - When should I provide a generic and non generic version of an interface? -


i understand difference between deep , shallow clone of object, according simon's answer on this (copy-constructor-versus-clone) question, generic , non-generic version should supplied. why?

you can define 2 interfaces, 1 generic parameter support typed cloning , 1 without keep weakly typed cloning ability when working collections of different types of cloneable objects:

i mean it's trivial enough make different interfaces, in generic-heavy paradigm of modern c#, finding difficult come valid reason why ever want use non-generic , weakly-typed version. heck, can have t:object , same thing!

i write interfaces this:

public interface ishallowcloneable {     object clone(); }  public interface ishallowcloneable<t> // should derive ishallowcloneable? {     t clone(); }  public interface ideepcloneable {     object clone(); }  public interface ideepcloneable<t> // should derive ideepcloneable? {     t clone(); } 

and class implement this:

public class fooclass : ideepcloneable<fooclass> {     // implementation } 

as quote says, if want able work collections of generic types produced same generic definition different type parameters have provide non-generic interface.

consider list of ideepcloneable<widget> , ideepcloneable<gadget> instances. can make list<object>, can't clone them unless resort runtime type checks.

if want able clone (or in general, access in other manner) these items polymorphically need them typed such static type offers minimum public interface let job. in case, make our list list<ideepcloneable>.

if don't intend use these interfaces polymorphically having non-generic ideepcloneable doesn't offer anything.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -