tags:

views:

64

answers:

5

I'm a bit confused about

The purpose of Marker Interface Vs Attributes.

Their purpose looks same to me(Pardon me if I'm wrong).

Can anyone please explain how do they differ in purpose?

+4  A: 

Some years ago, in the pre Java 5 era, Java didn't support attributes. Therefore, to "tag" a class or an interface so that they could be checked at runtime, you would use marker interfaces, which is basically an empty interface but you can still check if an instance can be casted to this interface.

In .NET, marker interfaces should not be used except for special use cases (such as allowing the use of extension methods), because attributes provide a better way to mark classes (and lots more) with metainformation. The same goes for Java 5 and newer, where annotations were introduced and should be used instead.

Lucero
I partly disagree. Marker Interfaces are still usefull and in use today. For example ICommand is a Marker Interface as is IDisposable, INotifyPropertyChanged and so on... and they are definitly not empty interfaces. But of course attributes are the prefered choice in .NET for adding meta data.
RonaldV
@RonaldV: IMO, marker interfaces are empty *by definition*. Therefore, if an interface is not empty, then it follows that must not be a marker interface. I would definitely say that none of the interfaces you mentioned are marker interfaces at all. This is particularly true in the case of IDisposable, which has methods that are called by language primitives.
Sean Reilly
Looked it up and yup you're correct. It seems like a lot of people are using the term very loosely and incorrectly.
RonaldV
+3  A: 

If you can use it, an Attribute is preferred. But there are a few features related to static typing only an interface can offer.

1) You can add extension methods to it
2) You can use it as a generic constraint

CodeInChaos
+1 for pointing out the advantages of marker interfaces.
TomTom
A: 

In .net Marker interface is not needed, because .net has Custom Attributes, so you can directly add custom metadata to class.

Andrey
-1 because there ARE differences practically, as others have pointed out rightly: Generics limitation and extension methods that utilize the marker interface.
TomTom
@TomTom yes, it is very clever to wait while all answers are given and start giving downvotes. Very contributing.
Andrey
+2  A: 

Here are some advantages of both.

Marker interfaces:

  • are a bit easier to check for using dynamic type checks (´obj is IMarker´);
  • allow for functional and data extensibility in the future (i.e. turning a “marker” interface into a “full” interface that actually declares some members);
  • can be used in generic type constraints;

On the other hand, attributes:

  • provide a clearer separation of metadata;
  • allow for specifying additional information via their constructors or properties;
  • allow for multiple application to an entity;
  • are general-purpose in terms of applicability to different kinds of entities, not just classes;

It heavily depends on the particular application's architecture and design whether it's appropriate to use a marker interface or an attribute in a particular case.

Ondrej Tucny
+2  A: 

As an aside, it is much quicker to check for an interface, as the CLI is designed for that and has opcodes for it (as does c#: is/as).

Checking attributes requires reflection; much slower.

Looking though the other answers most points are covered, but also: attributes are much more limited in terms of what values you can pass into them; mainly primitives and string.

Marc Gravell
If reflection for attributes should become a performance problem one can easily use a dictionary with the Type as key as a cache. So the performance benefit alone usually shouldn't make one use the interface instead of an Attribute.
CodeInChaos
Even dictionary access is very slow compared to a simple RTTI check
Marc Gravell