top
upGrad KnowledgeHut SkillFest Sale!

Search

C# Tutorial

 Attributes are a method of using metadata with code such as methods, assemblies, structures, enumerators, types. properties etc. The attributes are specified by using declarative tags that are created using square brackets placed before the required element.Attribute UsesSome of the major uses of attributes are given as follows:Attributes can be used to describe the COM properties of methods, classes, interfaces etc.The WebMethod attribute in web services can be used to mark methods with the help of attributes.Unmanaged code can be called using attributes with the DLLImport Attribute class.The assembly can be described in terms of version, trademark, title, description etc. using attributes.Attributes can be used to describe the way to map between class members and XML codes. This is for XML serialization.Attributes can be used to specify the different characteristics that are used to manage the security.The class members that need to be serialized for persistence are described using attributes.Attribute SyntaxThe syntax for attribute specification is given as follows:[ attributeName (parameter_1, parameter_2…..parameter_n) ] ElementsIn the above syntax, the attributeName specifies the class name of the attribute which is followed by zero or more parameters. All of this data is specified in square brackets.Predefined AttributesThere are a number of attributes in the System.Attribute base class library. Some of them are given as follows:Source: MSDNAttributesDescription[WebMethod]This is used to create XML web services. Also, the marked method is invoked by the HTTP request.[Obsolete]Any member or type can be marked as deprecated using [Obsolete]. The compiler issues a warning if Obsolete is used.[Serialization]A class persists with its current state by marking this attribute.[NonSerialization]A class or field should not be persisted using this attribute.[DllImport]This allows a call by the code to the unmanaged library.A program that demonstrates one of the predefined attributes i.e. the Obsolete attribute in C# is given as follows:Source Code: Program to implement Obsolete attribute in C#using System; namespace AttributesDemo {    class Example    {        static void Main(string[] args)        {            Console.WriteLine("Program for Attributes");            DemoMethod();        }        [Obsolete("Don't use the DemoMethod()",false)]        public static void DemoMethod()        {            Console.WriteLine("Inside DemoMethod()");          }    } }The output of the above program is as follows:main.cs(10,13): warning CS0618: `AttributesDemo.Example.DemoMethod()' is obsolete: `Don't use the DemoMethod()' Program for Attributes Inside DemoMethod()Now let us understand the above program.The Main() function calls the DemoMethod(). The message in the Obsolete attribute is "Don't use the DemoMethod()". Also false is displayed after that. This means that the compiler issues a warning. The code snippet for this is given as follows:static void Main(string[] args)        {            Console.WriteLine("Program for Attributes");            DemoMethod();        }        [Obsolete("Don't use the DemoMethod()",false)]        public static void DemoMethod()        {            Console.WriteLine("Inside DemoMethod()");          }
logo

C# Tutorial

Attributes in C#

 Attributes are a method of using metadata with code such as methods, assemblies, structures, enumerators, types. properties etc. The attributes are specified by using declarative tags that are created using square brackets placed before the required element.

Attribute Uses

Some of the major uses of attributes are given as follows:

  1. Attributes can be used to describe the COM properties of methods, classes, interfaces etc.
  2. The WebMethod attribute in web services can be used to mark methods with the help of attributes.
  3. Unmanaged code can be called using attributes with the DLLImport Attribute class.
  4. The assembly can be described in terms of version, trademark, title, description etc. using attributes.
  5. Attributes can be used to describe the way to map between class members and XML codes. This is for XML serialization.
  6. Attributes can be used to specify the different characteristics that are used to manage the security.
  7. The class members that need to be serialized for persistence are described using attributes.

Attribute Syntax

The syntax for attribute specification is given as follows:

[ attributeName (parameter_1, parameter_2…..parameter_n) ]
Elements

In the above syntax, the attributeName specifies the class name of the attribute which is followed by zero or more parameters. All of this data is specified in square brackets.

Predefined Attributes

There are a number of attributes in the System.Attribute base class library. Some of them are given as follows:

Source: MSDN

AttributesDescription
[WebMethod]This is used to create XML web services. Also, the marked method is invoked by the HTTP request.
[Obsolete]Any member or type can be marked as deprecated using [Obsolete]. The compiler issues a warning if Obsolete is used.
[Serialization]A class persists with its current state by marking this attribute.
[NonSerialization]A class or field should not be persisted using this attribute.
[DllImport]This allows a call by the code to the unmanaged library.

A program that demonstrates one of the predefined attributes i.e. the Obsolete attribute in C# is given as follows:

Source Code: Program to implement Obsolete attribute in C#

using System;
namespace AttributesDemo
{
   class Example
   {
       static void Main(string[] args)
       {
           Console.WriteLine("Program for Attributes");
           DemoMethod();
       }
       [Obsolete("Don't use the DemoMethod()",false)]
       public static void DemoMethod()
       {
           Console.WriteLine("Inside DemoMethod()");  
       }
   }
}

The output of the above program is as follows:

main.cs(10,13): warning CS0618: `AttributesDemo.Example.DemoMethod()' is obsolete: `Don't use the DemoMethod()'

Program for Attributes
Inside DemoMethod()

Now let us understand the above program.

The Main() function calls the DemoMethod(). The message in the Obsolete attribute is "Don't use the DemoMethod()". Also false is displayed after that. This means that the compiler issues a warning. The code snippet for this is given as follows:

static void Main(string[] args)
       {
           Console.WriteLine("Program for Attributes");
           DemoMethod();
       }
       [Obsolete("Don't use the DemoMethod()",false)]
       public static void DemoMethod()
       {
           Console.WriteLine("Inside DemoMethod()");  
       }

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

austin faith

Avery good write-up. Please let me know what are the types of C# libraries used for AI development.

kariya arti

very satisfied!!

jean-Francois Michaud

Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).

Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial