Interfaces Lack Data

Why does having two known types blow up for being conflicting data contract types if the types are two different interfaces?

An interface only contains declarations for its members. The interface doesn't contain any data fields and any implementation for the interface is located in the class that implements the interface.

When you try to serialize an interface type, any concrete class might walk up having implemented the interface. That concrete class might be holding any kind of data or none at all. Essentially, this makes an interface type no different than the type Object (or in the schema world, the type Any). In fact, that's exactly what the interface is as far as the serializer is concerned. If you have two interfaces, they're both equivalent to Object, which means they will conflict.

The only caveat there is that an interface type for one of the known collection types will turn into the corresponding concrete type for that collection type. It's still easy to get yourself into trouble with collections but at least it only affects pairs of types that are at least superficially similar.

Skip to main content