What is the difference between metadata annotations in C# and Java?

What is the difference between metadata annotations in C# and Java?

Metadata annotations are a method of extending programming language capabilities. Annotations can take the form of directives requesting that the runtime

  • extend type cabilities;
  • perform specific tasks;
  • provide information about an item.

C# attributes

C# attributes allow a developer to add annotations—that is, metadata—to a member variable, method, module, parameter, or type. The following are brief descriptions of a few intrinsic .NET attributes which can be employed to extend C# capabilities:

  • [FlagsAttribute] specifies that an enum supports bitwise operations. This is useful for enumerations with multiple value targets.
  • [MethodImpl(MethodImplOptions.Synchronized)] specifes that multiple thread access to a method is controlled by a lock designed to prevent concurrent access problems. This is analogous to the synchronized Java keyword.
  • [Serializable] marks a class as serializable. It is the C# analogue of a Java class which implements the Serializable interface.
  • [WebMethod] specifies—along with ASP.NET—that a method will be—automatically—made available as a Web service. The Java equivalent involves a complex configuration of Java Enterprise Edition, JAXP, and UDDI plus creating an Enterprise JavaBean—a bean class implementing two interfaces and a deployment descriptor.

Java annotations

Java annotations provide a way to add annotations—that is, metadata—to a local variable, member, method, package, parameter, or type. The Java system supports three built-in annotations as follows:

  • @Deprecated flags a method as deprecated—obsolescent—and causes a compiler warning to be generated.
  • @Override specifies that a method will override a base class method. If the annotated method does not do so, then a compiler error will result.
  • @SuppressWarnings is used to suppress specific compiler warnings. This annotation can accept the name of the specific warning message to be suppressed.

Custom annotations and attributes

In C#, custom attributes can be created by developers by subclassing the System.Attribute.

In the Java system, custom annotations can be created by developers using the @interface annotation.

Reflection

In C#, the attributes of a class, field, method, or module can be accessed using reflection. Use this feature to obtain class metadata or to determine at runtime whether a class supports certain behaviors.

In the Java system, class, field, method, or module annotations can be accessed via reflection. But, C# attributes and Java annotations differ in that only in the Java system can annotations be put on annotations.

See also

About Nguyễn Viết Hiền

Passionate, Loyal
This entry was posted in C#, Java, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s