When should I use attribute in C#?

C#Attributes

C# Problem Overview


I saw some of the examples of utilize attribute, e.g. (as a map for dynamic factory) http://msdn.microsoft.com/en-us/magazine/cc164170.aspx

Just wondering what is the advantage of using attribute? I can find the reference on http://msdn.microsoft.com/en-gb/z0w1kczw(VS.80).aspx however, I am not sure when and why should I try to use it.

C# Solutions


Solution 1 - C#

In the .NET Framework, attributes can be used for many reasons -- like

  • Defining which classes are serializable

  • Choosing which methods are exposed in a Web service

Attributes allow us to add descriptions to classes, properties, and methods at design time that can then be examined at runtime via reflection.

Consider this example:

Say you have a class which has a method from older version which is still in use for any reason and now you have come up with a new version of the class which makes fantastic use of Generic List and LINQ and has a new method for similar purpose. You would like developers to prefer the new one provided in the later version of your library. How will you do that ? One way is to write in the documentation. A better way is to use attribute as follow.

public class AccountsManager
{
  [Obsolete("prefer GetAccountsList", true)]
  static Account[] GetAccounts( ) { }    
  static List<Account> GetAccountsList( ) { }      
}

If an obsolete method is used when the program is compiled, the developer gets this info and decides accordingly.

> AccountManager.GetAccounts() is obsolete: > prefer GetAccountsList

We may also create and add Custom Attributes as per requirements.

Reference :


Hope this helps

Solution 2 - C#

My recommendation: use attributes to state facts about mechanisms, but not to model aspects of your business domain.

More details:

https://docs.microsoft.com/en-us/archive/blogs/ericlippert/properties-vs-attributes

Solution 3 - C#

Attributes are appropriate when you want to attach metadata to your classes or class members, as well as when applying a common behaviour without having to implement a certain interface for each unit that shares the behaviour. The latter is an example of aspect-oriented programming.

Solution 4 - C#

Consider an attribute as metadata about the method or property it belongs to. It tells something more about a member.

Solution 5 - C#

The .NET Framework predefines and uses attribute types to control run-time behavior of the application.

Consider [webmethod] attribute, at runtime framework resolves this attribute and determine this method going to be exposed in a webservice.

The same way, you can write your custom attributes to control the behaviour of your application at runtime. Attributes can target classes,methods,properties,delegate, enum, event, field...

To resolve the attribute at runtime, you must use reflection.

Checkout the MSDN link for more details.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionccppjavaView Question on Stackoverflow
Solution 1 - C#AsadView Answer on Stackoverflow
Solution 2 - C#Eric LippertView Answer on Stackoverflow
Solution 3 - C#Anders FjeldstadView Answer on Stackoverflow
Solution 4 - C#Gerrie SchenckView Answer on Stackoverflow
Solution 5 - C#RameshVelView Answer on Stackoverflow