Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use attribute in C#?

Tags:

c#

attributes

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.

like image 904
ccppjava Avatar asked Feb 19 '10 09:02

ccppjava


People also ask

What is the use of attribute in C?

The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.

Why we are using attributes?

Attributes provide a way of associating information with code in a declarative way. They can also provide a reusable element that can be applied to a variety of targets. Consider the [Obsolete] attribute. It can be applied to classes, structs, methods, constructors, and more.

When should I use attributes C#?

Attributes are used in C# to convey declarative information or metadata about various code elements such as methods, assemblies, properties, types, etc. Attributes are added to the code by using a declarative tag that is placed using square brackets ([ ]) on top of the required code element.

What is attribute in C Plus Plus?

Attributes are modern ways in C++ to standardize things if their code runs on different compilers. Attributes are used to provide some extra information that is used to enforce conditions (constraints), optimization and do specific code generation if required.


2 Answers

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 :

  • Using Attributes in C#

Hope this helps

like image 111
Asad Avatar answered Oct 20 '22 11:10

Asad


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

like image 39
Eric Lippert Avatar answered Oct 20 '22 13:10

Eric Lippert