Attributes of CLR Vir C #

Source: Internet
Author: User
Tags access properties

CLR provides two types of attributes: No parameter attribute and with parameter attribute. The former is simply called an attribute, and the latter is usually called an indexer in C.

No parameter property:

1. common attributes are usually combined with private fields to encapsulate class information.

2. attributes can be viewed as fields with logic, ensuring security and convenient operations.

3. CLR supports instances, static, abstract, and virtual attributes.

4. You can define any access level or an interface.

5. The parameter-free attribute contains a name and a return type (cannot be void), and cannot be overloaded.

6. When defining an attributeCodeIn fact, the corresponding get_xx and set_xx methods are automatically generated. Operations on attributes are actually operations on this method.

7. Generate a single set_xx or get_xx Method Based on the attribute definition (read-only or write-only.

Code
Class Propertydemo
{
Static   Void Main ()
{}

Private IntAge;

Public   Int Age
{
Get { Return Age ;}
Set {Age = Value ;}
}
Private   String Name;

Public StringName
{
Get{ReturnName ;}
Set{Name=Value ;}
}
}

Il:

 

Code
Public   Void Set_age ( Int Value)
{
This . Age = Value;
}
Public   Int Get_age ()
{
Return   This . Age;
}

Therefore, you can use this method to encapsulate fields in languages that do not support attributes.

8. Attributes cannot be passed to the method as out/Ref, and fields can.
9. If too many statements are executed in the attribute, it takes a lot of time. In this case, the method is preferred. For example, thread synchronization and remoting.
10. For Multiple Access properties, the values returned each time may be different, and the values of fields will not change each return. For example, the system. datetime attribute -- ms will change it to a method later.

Parameter attributes

1. Use the this keyword to define the property with parameters. The get method can accept one or more parameters, and the set method can accept two or more parameters. Set_item, get_item.

2. C # is actually an overload of the [] operator.

Code
Class Propertydemo
{
Static   Void Main ()
{
Propertydemo demo =   New Propertydemo ();
Console. writeline (Demo [ 1 ]);
}

Int[] Arr=New Int[] {1,2,3,4};

Public   Int   This [ Int Index]
{
Set  
{
Arr [Index] = Value;
}
Get
{
Return Arr [Index];
}
}
}

Il:

Code
Public   Void Set_item ( Int Index, Int Value)
{
This . Arr [Index] = Value;
}
Public   Int Get_item ( Int Index)
{
Return   This . Arr [Index];
}

3. The property with parameters (Indexer) can be reloaded (it cannot be composed of different return types ).

Code
Public   Int   This [ Int Index]
{
Set {}
}

Public Int This[DoubleIndex]
{
Set{}
}

4. You can use features to change the name of the indexer in the metadata.

Code
Using System. runtime. compilerservices;

Public   Sealed   Class Bitarray
{
[Indexername ( " Bit " )]
Public   Bool   This [ Int Bitpos]
{
Set {}
}
}

The generated method name is get_bit and set_bit.

5. The indexer can be used to define collection classes.

Http://blog.csdn.net/JustLovePro/archive/2008/07/11/2639510.aspx

Performance

1. attributes are embedded with their code during compilation, reducing the performance loss of method calls during runtime.

2. Inner China Unicom often runs in the case of a small amount of method code and cannot be manually controlled by Nei.

3. inline ensures the same performance as the field during attribute running

4. Note: because there is no inline attribute method in the debug phase, the release is used for inline, so the version after release is faster ~

5. fields are not affected.

Attribute access level

When we define an attribute as public, the get_xx method is actually defined as public, and the set_xx method is protected.

Generic attributes are not supported.

 

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.