Attributes are elements that allow you to add declarative information to your programs. This declarative information is used for various purposes during runtime and can be used at design time by application development tools. Attributes are also used extensively in securing .NET assemblies, forcing calling code to be evaluated against pre-defined security constraints. Attribute declared using the ‘[‘and ‘]' brackets before any type declaration.


 


The following are the advantages the attributes provide:



  • Declarative information to your program

  • This information can be used at runtime or at compile time by programmers.

  • DllImportAttribute: This attribute provide information to communicate with the Win32 libraries.

  • ObsoleteAttribute: This provides compile time information to the developer that this method is now deprecated.

  • Attribute add what is called metadata to your program.

  • Attributes are classes that can be written in C# and used to decorate your code with declarative information.

  • Many attributes have parameter lists that allow inclusion of additional information that customizes a program even further.


Examples of Attributes:-


 


[ Obsolete ]


public String firstDeprecatedFX(){}


[ ObsoleteAttribute ]


public String secondDeprecatedFX(){}


Obsolete ( "Don't Use This Method." )]


public String thirdDeprecatedFX()


[ STAThread ]


static void Main () {}


 


Note:- This attribute declare that this is the entry point for the application. This indicates that C# program should communicate with unmanaged COM code using the single threading apartment. The parameter provide additional information about the attribute


 


Example To Demonstrate Attributes


 


The following is the screen appeared when run the form 15 program in the source.


 


 




 


 


ClsAttribute15.cs


 


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace cSHARPEXAMPLES


{


  class ClsAttribute15


    {


    [ Obsolete ]


    public String firstDeprecatedFX()


        {


      return "Call firstDeprecatedFX" ;


        }


 


    [ ObsoleteAttribute ]


    public String secondDeprecatedFX()


        {


      return "Call secondDeprecatedFX" ;


        }


 


    [ Obsolete ( "Don't Use This Method." )]


    public String thirdDeprecatedFX()


        {


      return "Call thirdDeprecatedFX" ;


        }


 


    }


}


 


 


Form15.cs


 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


 


namespace cSHARPEXAMPLES


{


  public partial class Form15 : Form


    {


    public Form15()


        {


          InitializeComponent();


        }


    private void button1_Click( object sender, EventArgs e)


        { //Attribute : use to create method deprecated


            ClsAttribute15 obj = new ClsAttribute15 ();


      obj.firstDeprecatedFX();


      //Deprecated Methods


      obj.secondDeprecatedFX();


      MessageBox .Show(obj.thirdDeprecatedFX());


            }


      }


}


 


 


Output:


 


Clicking On: Attribute fire up Button


 



                    

Copyright © 2012 VisualBuilder. All rights reserved