C # What does 3.0 bring to us (2) -- Automatic attributes

Source: Internet
Author: User
Tags mscorlib

Public int ID {Get; protected set ;}
Public string name {Get; set ;}
Is this an attribute declared in the interface?
No, this can also be a class attribute, automatic attribute.
If C #3.0 is the biggest change, it is that the encoding method is more user-friendly and programmers can become more lazy. Automatic attributes are also specific manifestations of this feature.
Compare two pieces of code
C #2.0

Public Class
{
Private int _ id;
Private string _ name;
Public int ID
{
Get
{
Return _ id;
}

Protected set
{
_ Id = value;
}
}

Public string name
{
Get
{
Return _ name;
}
Set
{
_ Name = value;
}
}
Public A (int id)
{
This. _ id = ID;
}
}

C #3.0 public Class B
{
Public int ID {Get; protected set ;}
Public string name {Get; set ;}
Public B (INT ID)
{
This. ID = ID;
}
}

In C #, we recommend that you use attributes instead of public variables. In our project, we also try to define the fields that need to be made public as attributes, instead of the redundant code above. No matter whether you have any special processing for this attribute, you must first define a private variable and return or assign a value to it in the get or set methods. This increases the amount of code and difficulty in reading.
The automatic attributes that come with 3.0 allow us to easily define a simple attribute (which does not require special processing on the field), and the private variables used by the system are generated. Reduced workload and difficulty in reading.

Let's take a look at what the compiler has done for us.
Use il discycler to see what happened.

We can see that except for private variables, they are all the same.
Let's take a look at the differences between the two variables.
C #2.0
. Field private int32 _ id
# C 3.0:
. Field private int32 '<ID> K _ backingfield'
. Custom instance void [mscorlib] system. runtime. compilerservices. compilergeneratedattrictor:. ctor () = (01 00 00)
Reflection?
Msdn describes compilergeneratedattribute in this way.
Distinguishes a compiler-generated element from a user-generated element. This class cannot be inherited.
Distinguish between the elements generated by the compiler and those generated by the user. This class cannot be inherited.
It means that the above two sides of the Il code are no different, but the private variables in the second are marked as generated by the compiler.
Let's take a look at two set_name
C #2.0
. Method public hidebysig specialname instance void
Set_name (string 'value') cel managed
{
// Code size 9 (0x9)
. Maxstack 8
Il_0000: NOP
Il_0001: ldarg.0
Il_0002: ldarg.1
Il_0003: stfld string windowsformsapplication1.a: _ name
Il_0008: Ret
} // End of method A: set_name

# C 3.0:
. Method public hidebysig specialname instance void
Set_name (string 'value') cel managed
{
. Custom instance void [mscorlib] system. runtime. compilerservices. compilergeneratedattrictor:. ctor () = (01 00 00)
// Code size 8 (0x8)
. Maxstack 8
Il_0000: ldarg.0
Il_0001: ldarg.1
Il_0002: stfld string windowsformsapplication1. B: '<Name> K _ backingfield'
Il_0007: Ret
} // End of Method B: set_name

The difference is that there is only one more sentence.
. Custom instance void [mscorlib] system. runtime. compilerservices. compilergeneratedattrictor:. ctor () = (01 00 00)

Now, you can use it with confidence.

The automatic attributes ultimately bring us the public class B encoded in two blending modes.
{
Private int _ age;
Public int ID {Get; protected set ;}
Public string name {Get; set ;}
Public int age
{
Get
{
Return _ age;
}
Set
{
If (value <0 | value> 120)
New exception ("age out of range. ");
Else
_ Age = value;
}
}
Public B (INT ID)
{
This. ID = ID;
}
}

(The younger brother is not easy to learn. You are welcome to correct your mistakes .)

Related Article

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.