There is an existing class from a third-party library I want to reuse, but it does not implement some of the required interfaces. I'm thinking of wrapping it inside a struct to fulfill the required interfaces (basically like how Scala does it). The reason why I prefer struct over class for this use case is because it only stores one thing: the wrapped object, and it probably won't incur performance penalties? But I'm not sure if boxing would occur if I pass a struct to some method that takes in an interface? And are there any other drawbacks about this approach?
Microsoft gives very clear and straightforward reasons where you should use a struct instead of a class:
√ CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.
X AVOID defining a struct unless the type has all of the following characteristics:
https://msdn.microsoft.com/en-us/library/ms229017.aspx
And seems like it's not your case. And btw yes, boxing will occur if you pass a struct to a method that takes an interface. Also take into account that structures don't support inheritance and they can't have explicit parameterless constructors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With