Search code examples
c#javaattributesannotationslanguage-design

Are C#'s attributes better designed than Java's annotations?


After reading this old post from Clinton Begin (creator of iBatis) I really wonder if his claims about annotations vs. attributes are widely accepted or if there is disagreement about it.

His points are:

  • Annotations are not extendable
  • No support for positional arguments
  • Java-unlike definition syntax
  • annotation is not a keyword (unlike enum)

Do those claims have merit and how does C# improve on that?


Solution

  • Well taken one by one those points obviously have merit:

    • Attributes are classes you can extend and query as you wish. You can even add your own!
    • Position arguments (I'm guessing you mean named arguments) are indeed possible with C#, with full Intellisense support.
    • Can't really comment on how weird it looks, although coming from Razor it makes me think of macro expansion.
    • Attributes don't have a keyword either, they're just a normal class.

    That doesn't make Java's implementation worse, since they had a different goal in mind: backwards compatibility. C# had the advantage of building the language from scratch (and then progressing forward instead of maintaining strict backwards compatibility).

    As a nit-pick though, nothing you or I mentioned are part of C#, they are part of the .NET runtime and can be found equally well in VB.NET and F#.