Saturday, 28 May 2016

attributes

An attributes is a declarative tag that is used to convey information to runtime about the behaviors of elements like classes, methods, structures, enumerators, assemblies, properties etc…
You can add declarative information to a program by using an attributes.
In simple terms attribute is used to tell more about element.
You can declare attributes using “[]” square brackets placed above the element.
Syntax:
[Attribute (parameters)]
Element

Predefined Attributes
.Net framework provides three types of pre-defined attributes.
AttributeUsage(AttributeTarget: to set class, property, fields…)
Conditional
Obsolete (out of date, old, no longer to used or produced)

AttributeUsage
It is used to define how custom attribute can be used. It is specified that type of elements to be allowed which attribute can be applied.
Syntax:
[AttributeUsage(AttributeTargets.All,AllowMultiple=true,Inherited=false)]
Attribute-Targets: this parameter gives a validation for class, property, field, enum, delegates, methods, assembly, and all
So using attribute targets you can use for specific elements only.
Allow-Multiple: This parameter has Boolean value. If it is true then you can use on multiple elements, or false used only on single element.
Inherited: This parameter has Boolean value. If it is true the attributes is inherited by derived class or else the default value is false.

You can also set multiple attribute-targets using “|”.
Syntax:

[AttributeUsage(AttributeTargets.Class| 
    AttributeTargets.Property | 
    AttributeTargets.Method,
    AllowMultiple=true,
    Inherited=false)]
Example:

using System;

class Program
{

    static void Main()
    {
        myClass obj = new myClass();
        obj.Name = "SampleProgram";
    }
}

//this is myclass
class myClass
{
    //has property
    [Help(maxLength=10)]
    public string Name { get; set; }
}

//Create help class inherit to attribute
[AttributeUsage(AttributeTargets.Property,AllowMultiple=true,Inherited=true)]
class Help : Attribute
{
    public int maxLength { get; set; }
}

This predefined attribute marks a conditional method whose execution depends on a specified preprocessing identifier.
Syntax:

[Conditional(
   conditionalSymbol
)]
Example:

[Conditional("DEBUG")]

It is used to set or marked a program entity that it is no longer in used or it is old.
It tells to compiler that discard particular element.
Syntax:

[Obsolete(message="",iserror)]
For example: if you are going to modify project which has list of class in that list of methods is there now if you want to create a new method to do certain things and still you want to retain the old method in the class. you can mark as obsolete to tell compiler to discard.

[Obsolete(message="This method is no longer in used.")]
Example:

using System;

class Program
{
    static void Main()
    {
        myClass obj = new myClass();

        //if you include this statment it gives error
        //it is no longer in used...
        obj.Add("Sample");

        //Success fully run.
        obj.Add(1, "Sample");
    }
}
class myClass
{
    [Obsolete("It is no longer used.",true)]
    public void Add(string name)
    {
        Console.WriteLine("Print: {0}", name);
    }

    public void Add(int id,string name)
    {
        Console.WriteLine("Print: Id: {0} Name:{1}",id, name);
    }
}

Notes:
Attribute is just for declaration tag, it is not a validation.
You can create your custom attribute with the help of Attribute abstract class.
you can also visit:  www.mybook-lang.com

No comments:

Post a Comment