The attribute property in C #

Source: Internet
Author: User

In C #, attribute is an element modifier that exists as a source code for a program, because sometimes we need to add some descriptive information to our code. When we do not want to use annotations or internal code to describe the information that is compiled as attribute code, the compiler will generate them into the metadata.

At the same time, attribute is also an object.

1[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method | 2Attributetargets.returnvalue | Attributetargets.property, AllowMultiple =true, inherited =true)] 3  PublicclassCountryattribute:attribute4    { 5        6 PublicCountryattribute () {}7 8 PublicCountryattribute (stringname)9         { Ten This. Name =name;  One         }  A  - PublicintPlayercount {Get;Set; }  -          PublicstringName {Get;Set; }  the     }  -  -[Country (" China")]      -[Country ("America")]      +      PublicclassSportsman -     {  + PublicstringName {Get;Set; }  A       at[Country (Playercount =5)]          -          Public VirtualvoidPlay () {} -      }  -      -      PublicclassHoopster:sportsman -     {  in  PublicOverridevoidPlay () {} -}

It is very convenient to use attribute. He is a program that will be compiled, but can be used like a comment. Rather than using annotations, attribute can identify the results of functions, return values, and so on in the execution results, and implement some more complex identity functions. However, according to some data attribute itself is not a modifier, but a class that is instantiated by the class, which can be seen by anti-compilation.

The attribute property in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.