I seem to remember reading something about how it is bad for structs to implement interfaces in CLR via C#, but I can’t seem to find anything about it. Is it bad? Are there unintended consequences of doing so?
public interface Foo { Bar GetBar(); } public struct Fubar : Foo { public Bar GetBar() { return new Bar(); } }
There are several things going on in this question…
It is possible for a struct to implement an interface, but there are concerns that come about with casting, mutability, and performance. See this post for more details: https://learn.microsoft.com/en-us/archive/blogs/abhinaba/c-structs-and-interface
In general, structs should be used for objects that have value-type semantics. By implementing an interface on a struct you can run into boxing concerns as the struct is cast back and forth between the struct and the interface. As a result of the boxing, operations that change the internal state of the struct may not behave properly.