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

Popular posts from this blog

java - How to pass parameters between Request-Scoped Controllers? -

actionscript 3 - Flex: moving a point with rotation doesnt change the point XY? -

string - what is the difference between "some" == "some\0" and strcmp("some","some\0") in c++? -