Monday, March 14, 2016

C# Attributes Overview [What are attributes and why do we use it?]

C# Attributes Overview

Attributes are elements that allow you to add declarative information to your programs. This declarative information is used for various purposes during run-time and can be used at design time by application development tools.

Attributes provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). Once associated with a program entity, the attribute can be queried at run time and used in any number of ways.(msdn)

=> It’s often necessary to associate information (metadata) with types and members, e.g.
           -Documentation URL for a class
           -Transaction context for a method
           -XML persistence mapping
           -COM ProgID for a class
=> Attributes allow you to decorate a code element (assembly, module, type, member, return value and parameter) with additional information

Attributes are generally applied physically in front of type and type member declarations. They're declared with square brackets, "[" and "]", surrounding the attribute such as the following ObsoleteAttribute attribute:


The "Attribute" part of the attribute name is optional. So the following is equivalent to the attribute above:


A deprecated class or method is like that. It is no longer important. It is so unimportant, in fact, that you should no longer use it, since it has been superseded and may cease to exist in the future.

The Obsolete attribute marks a program entity as one that is no longer recommended for use. Each use of an entity marked obsolete will subsequently generate a warning or an error, depending on how the attribute is configured. 

[Obsolete] attribute is a pretty useful concept in the .NET Framework, designed to mark parts of our code that is planned to be phased out and its usage should be avoided. It makes a lot of sense to mark methods or types as obsolete, e.g. when we drop support for some parts of our API and the code is planned to be removed when all the customers are migrated, or when we've restructurized our code so that there's a newer approach for particular problem and we just had no time or possibility to upgrade all the usages, etc.

For example below is a simple class where “MyFirstMethod” is decorated by the “Obsolete” attribute. So when developer call this class they are alerted that “MyFirstMethod” is obsolete and MyFirstMethod is deprecated, please use "MyFirstListMethod" instead.


namespace DemoAttributes
    public class MyBasicAttributesDemo
        [Obsolete("MyFirstMethod is deprecated, please use MyFirstListMethod' instead")]
        public void MyFirstMethod()
            //Console.WriteLine("Obsolete Attribute...");

        public void MyFirstListMethod()
            //Console.WriteLine("New Method...");


namespace DemoAttributes
    public class Program
        static void Main(string[] args)
            MyBasicAttributesDemo obj = new MyBasicAttributesDemo();


Overview & Some Attributes Description

  • Attributes are superior to the alternatives
  • Modifying the source language
  • Using external files, e.g., .IDL, .DEF

  • Attributes are extensible
  • Attributes allow to you add information not supported by C# itself

  • Not limited to predefined information

  • Built into the .NET Framework, so they work across all .NET languages
  • Stored in assembly metadata

  • Some predefined .NET Framework attributes

  • Attributes can be
  • Attached to types and members

  • Examined at run-time using reflection

  • Completely extensible
  • Simply a class that inherits from System.Attribute

  • Type-safe
  • Arguments checked at compile-time

  • Extensive use in .NET Framework
  • XML, Web Services, security, serialization, component model, COM and P/Invoke interop, code configuration etc....

Attribute Name
Should a property or event be displayed in the property window
Allows a class or struct to be serialized
Compiler will complain if target is used
Transactional characteristics of a class