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.
Some of the major uses of attributes are given as follows:
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.
There are a number of attributes in the System.Attribute base class library. Some of them are given as follows:
Source: MSDN
Attributes | Description |
---|---|
[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()"); }
Avery good write-up. Please let me know what are the types of C# libraries used for AI development.
very satisfied!!
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).
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
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
Leave a Reply
Your email address will not be published. Required fields are marked *