c# - Any way to constrain a generic interface to the type that implements it? -
public interface icloneable<t> { t clone(); } public foo: icloneable<foo> { public foo clone() { //blah } }
is there way constrain t type implements interface? (foo in case). nice enforce implementing icloneable homecoming instance of itself, , not random type fancies.
no, basically. can't generic constraints. also, can't stop them implementing interface multiple times different t (as long t satisfy where constraints, none in case).
there no where constraint allows restriction implementing type.
you kinda sorta can method parameter restriction, isn't satisfactory:
public static t superclone<t>(this t original) t : icloneable<t> {...} c# generics interface generic-constraints
No comments:
Post a Comment