Is it safe for structs to implement interfaces?

Viewed 77469

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(); } }
9 Answers

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://docs.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.

Structs are implemented as value types and classes are reference types. If you have a variable of type Foo, and you store an instance of Fubar in it, it will "Box it" up into a reference type, thus defeating the advantage of using a struct in the first place.

The only reason I see to use a struct instead of a class is because it will be a value type and not a reference type, but the struct can't inherit from a class. If you have the struct inherit an interface, and you pass around interfaces, you lose that value type nature of the struct. Might as well just make it a class if you need interfaces.

(Well got nothing major to add but don't have editing prowess yet so here goes..)
Perfectly Safe. Nothing illegal with implementing interfaces on structs. However you should question why you'd want to do it.

However obtaining an interface reference to a struct will BOX it. So performance penalty and so on.

The only valid scenario which I can think of right now is illustrated in my post here. When you want to modify a struct's state stored in a collection, you'd have to do it via an additional interface exposed on the struct.

There is very little reason for a value type to implement an interface. Since you cannot subclass a value type, you can always refer to it as its concrete type.

Unless of course, you have multiple structs all implementing the same interface, it might be marginally useful then, but at that point I'd recommend using a class and doing it right.

Of course, by implementing an interface, you are boxing the struct, so it now sits on the heap, and you won't be able to pass it by value anymore...This really reinforces my opinion that you should just use a class in this situation.

There are no consequences to a struct implementing an interface. For example the built-in system structs implement interfaces like IComparable and IFormattable.

Structs are just like classes that live in the stack. I see no reason why they should be "unsafe".

Related