Understanding Attributes In C#

What is Attribute?

Attribute is a class used to give additional declarative information for the class, method, property, indexer, etc. By using attributes you can identify the behavior of the class, method, property, indexer, etc.

Syntax:

[ Attribute_Name(argument(s))]

The following are the different types of attributes in C#:

  1. Obsolete:

    It is used to specify whether the class or method is deprecated or not.

    Obsolete attribute contains the following two arguments:

    i) Message
    ii) True/False

    If value is false, when deprecated method is used in the program then the program will execute with.

    If value is True, when deprecated method is used in application then compilation error will be displayed.

    Ex.
    1. using System;  
    2.   
    3. namespace Attribute {  
    4.     class Program {  
    5.         [Obsolete("display1 is deprecated, use display2"false)]  
    6.         public void display1() {  
    7.             Console.WriteLine("Visual Studio 2008");  
    8.         }  
    9.         public void display2() {  
    10.             Console.WriteLine("Visual Studio 2015");  
    11.         }  
    12.   
    13.         static void Main(string[] args) {  
    14.             Program obj = new Program();  
    15.             obj.display1();  
    16.             Console.Read();  
    17.         }  
    18.     }  
    19. }  

  2. Conditional:

    It is used to specify whether the method should be called or not.
    The conditional method must be a method in a class or structure declaration. A compile-time error occurs if the Conditional attribute is specified on a method in an interface declaration.

    The conditional method must have a void return type.

    Conditional attribute is in System.Diagnostics namespace.

    Ex.
    1. #define hello  
    2. using System;  
    3. using System.Diagnostics;  
    4.   
    5. namespace AttributeConditional {  
    6.     class Program {  
    7.         [Conditional("hello")]  
    8.         public void print() {  
    9.             Console.WriteLine("Happy coding...");  
    10.         }  
    11.         static void Main(string[] args) {  
    12.             Program obj = new Program();  
    13.             obj.print();  
    14.             Console.Read();  
    15.         }  
    16.     }  
    17. }  

  3. WebMethod:

    This attribute will be used in xml web services in ASP.NET. In xml web service if any method or function is having this attribute then the method can be exposed on web or the method will execute in any browser.

    Ex.
    1. [WebMethod]  
    2. Public string HelloWorld()   
    3. {  
    4.     return“Hello World”;  
    5. }  

  4. DllImport:

    Dll files are of two types: Managed dll and unmanaged dll. Old languages (such as Visual Basic 6.0, VC++, etc) and O.S. dll files are called unmanaged dll files. These will contain unmanaged code which will execute without CLR.

    Dot Net dll files are called managed dll files. It contains managed code which will execute through CLR.

    To use unmanaged dll files in managed code, DllImport attribute is used.

    Ex.
    1. using System;  
    2. using System.Runtime.InteropServices; // DllImport  
    3.   
    4. class App {  
    5.     [DllImport("lib.dll")]  
    6.     extern static int fun(int n);  
    7.   
    8.     static void Main() {  
    9.         Console.WriteLine(fun(0));  
    10.     }  
    11. }  

Creating user defined Attributes

User defined attribute can be used to give information about the class or method i.e whether the class is user defined class or abstract class, base class, etc. and for method you can specify whether it is user defined method, abstract method, virtual method, etc.

Attribute class in System namespace is a base class for custom attribute or user defined attribute.

Ex.

  1. using System;  
  2.   
  3. namespace CustomAttribute {  
  4.     class CustomAttribute: Attribute {  
  5.         private string s;  
  6.         public CustomAttribute(string str) {  
  7.             pstr = str;  
  8.         }  
  9.         public string pstr {  
  10.             get {  
  11.                 return s;  
  12.             }  
  13.             set {  
  14.                 s = value;  
  15.             }  
  16.         }  
  17.   
  18.     }  
  19.     class MyClass {  
  20.         [Custom("User defined method")]  
  21.         public void getString() {  
  22.             Console.WriteLine("VS.Net 2015");  
  23.         }  
  24.     }  
  25.     class Program {  
  26.         static void Main(string[] args) {  
  27.             MyClass obj = new MyClass();  
  28.             obj.getString();  
  29.             Console.Read();  
  30.         }  
  31.     }  
  32. }  

Up Next
    Ebook Download
    View all
    Learn
    View all