c# - Generic Type contraints with Or -
c# - Generic Type contraints with Or -
public t createform<t>() t: baseform, basemainform
i know above means t baseform
and basemainform
. possible create constraint t has either baseform
or basemainform
?
no, not allowed in c#. compiler uses generic constraint determine operations available on t
within generic method - allowing or look not type safe.
if need this, consider adding interface covering mutual parts of baseform
, basemainform
, , apply generic constraint. way, interface defines contract of method createform<t>
needs - , must create sure form's pass in implement interface.
something like:
public interface ibaseform { foo(); } class baseform : ibaseform {} class basemainform : ibaseform {} public t createform<t>() t : ibaseform
c# constraints generics
Comments
Post a Comment