What is an empty interface used for

Solution 1:

Usually it's to signal usage of a class. You can implement IMessage to signal that your class is a message. Other code can then use reflection to see if your objects are meant to be used as messages and act accordingly.

This is something that was used in Java a lot before they had annotations. In .Net it's cleaner to use attributes for this.


@Stimpy77 Thanks! I hadn't thought of it that way. I hope you'll allow me to rephrase your comment in a more general way.

Annotations and attributes have to be checked at runtime using reflection. Empty interfaces can be checked at compile-time using the type-system in the compiler. This brings no overhead at runtime at all so it is faster.

Solution 2:

Also known as a Marker Interface:

http://en.wikipedia.org/wiki/Marker_interface_pattern

Solution 3:

In java Serializable is the perfect example for this. It defines no methods but every class that "implements" it has to make sure, that it is really serializable and holds no reference to things that cannot be serialized, like database connections, open files etc.

Solution 4:

In Java, empty interfaces were usually used for "tagging" classes - these days annotations would normally be used.

It's just a way of adding a bit of metadata to a class saying, "This class is suitable for <this> kind of use" even when no common members will be involved.