With Xiaojing reading CLR via C # (11)-No parameter attribute, Indexer

Source: Internet
Author: User

We are no stranger to mentioning attributes. It is used just like accessing public data members, but it actually calls the corresponding internally defined method. By using attributes, Data encapsulation is maintained and access is convenient. Next, let's review the two attributes that the CLR allows to define: No parameter attributes and no parameter attributes (Indexer ).

I. No parameter Property 1. Define attributes

The parameter-free attribute is our most common attribute method. When assigning values, you can add certain logic judgments. Attribute definition is not complex. Let's look at an intuitive example:

Note:

    • The attribute must define the name and type, and the type cannot be void.
    • Attributes cannot be reloaded. We cannot define two attributes with the same name and different types.
    • Attributes need to be definedGetAndSetMethod to operate private supported fields in the class, such as _ name and _ age. The set method contains a hidden parameter calledValue, Indicating the value assigned to the attribute.
    • Read-only write attribute: You can omit set to define read-only attributes (such as the Count attribute), or omit get to define write-only attributes.
    • CLR supports static, instance, abstract, and virtual attributes. In this example, name and age are our most common instance attributes, while count is an example of static read-only attributes.

Call:When attributes are used, intelligent sensing is generated, just like using public fields:

Running result:

2. Compilation result

Use ildasm.exe to view metadata,

We found that the following items are missing:

① If the property contains the get accesser, the"Get _Attribute name", Such as get_age;

② If the property contains the set accessors, the"Set _Attribute name", Such as set_name;

③ The property definition items in the metadata include some tags and property types, and reference the get or set accessors method. In this way, the attributes and accessors are associated. For example, the Count attribute defines the item content:

3. automatically implemented attributes-AIP

AIP (automatically implemented property) is a more concise way to implement properties. For example, the student class above can be simplified:

The call method and running result are the same as before, so we will not go into details here.

Simplicity is good, but pay attention to the following points:

① Breakpoint debugging cannot be added to the get and set methods of AIP.

② The AIP attribute must be readable and writable at the same time. If you only define get or set, both must be explicitly implemented and AIP is not allowed.

③ Do not define AIP in the class to be serialized or deserialized. Because the runtime serialization engine persists the field name to the serialization stream, and this name may change during each compilation.

Ii. attributes with parameters -- Indexer

The indexer is an attribute of the accessors that contains parameters. C # exposes the indexer in an array style.

1. Define the indexer.

① Similar to the parameter-free attribute, the indexer also needs to define the get and set accessors, and the value keyword can be used in the Set accessors. The difference is that the get accessors Of The indexer must accept parameters.

② Use the this keyword to define the indexer.

Call: The indexer allows objects to be indexed in a way similar to an array.

2. Compilation result

Ildasm.exe.

After compilation, it is similar to the parameter-free attribute, except that the default name is used by the compiler for the accessors.Item:

① If the indexer contains the get accessors, the "get_item" method will be generated;

② If the indexer contains the set accessors, the "set_item" method will be generated;

③ Attribute definition items in metadata.

3. Notes

① The name of the item generated by default can be modified. You can add the attribute "indexername" to the indexer. For example:

② The indexer can be overloaded. In C #, a class can define multiple indexers, as long as the parameter set of The indexer is different.

③ The indexer can have multiple parameters, for example, when accessing a two-dimensional array.

④ The indexer can only be used for instances and cannot be used for static operations.

The end.

I finally finished writing. Ele. Me, eat, go home.

    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.