Wrappers for unrelated types to enable generic constraints in C# -


if have generic type needs contain bool or custom number type called number, bad idea create wrapper type bool , make both wrapper , number inherits same base class? instead of myclass<bool> have myclass<boolwrapper>?

it ensure myclass<righttype>, righttype bool wrapper, number type or common base class. myclass<wrongtype> not allowed. stands between , splitting myclass 2 classes, myclasswithbool , myclasswithnumber. reason due dsl making. might want have method avoid boilerplate code:

public void bind<t>(myclass<t> m) { ... } 

but allow giving wrong types, want able this:

public void bind<t>(myclass<t> m) t : baseclass { ... } 

then wouldnt need:

public void bind(myclasswithbool m) { ... } public void bind(myclasswithnumber m) { ... } 

take care, kerr.


Comments

Popular posts from this blog

javascript - JS causing window size to be bigger than necessary - Dropdown bug -

php - Calling a template part from a post -

How to mention the localhost in android -