Using implicit conversion as a substitute for multiple inheritance in .NET
Posted
by Daniel Plaisted
on Stack Overflow
See other posts from Stack Overflow
or by Daniel Plaisted
Published on 2010-04-18T18:24:15Z
Indexed on
2010/04/18
18:33 UTC
Read the original article
Hit count: 371
I have a situation where I would like to have objects of a certain type be able to be used as two different types. If one of the "base" types was an interface this wouldn't be an issue, but in my case it is preferable that they both be concrete types.
I am considering adding copies of the methods and properties of one of the base types to the derived type, and adding an implicit conversion from the derived type to that base type. Then users will be able treat the derived type as the base type by using the duplicated methods directly, by assigning it to a variable of the base type, or by passing it to a method that takes the base type.
It seems like this solution will fit my needs well, but am I missing anything? Is there a situation where this won't work, or where it is likely to add confusion instead of simplicity when using the API?
© Stack Overflow or respective owner