C#3.0 Series: Automatic Property

Source: Internet
Author: User

Previous practice:

Before c#3.x out, I believe you have been accustomed to defining and implementing a public property through a private field + public-property hairstyle. is implemented as follows.

1class person
2  {
3    private int age;
4    private string _name;
5    public int Age
6    {
7      get { return age; }
8      set { age = value; }
9    }
10    public string Name
11    {
12      get { return _name; }
13      set { _name = value; }
14    }
15  }

Obviously you can in the Set/get code block in the property, we can define our business logic without restriction, but in most cases, we all operate on a defined field directly like the code above: Read and write to it. But if we need to encapsulate a lot of data depending on the needs of the project, such as the Business entity class, we need to define a property for different types of data, so it must be annoying to keep repeating the work.

Automatic Property Overview

After c#3.x out, we can simplify our operation by automatic property. e.g.

1class Employee
2  {
3    //public string Name { get; } error
4    public string Name { get; set; }
5    public int Age{get; private set;}
6    public Employee(string name,int age )
7    {
8      this.Name = name;
9      this.Age = age;
10    }
11  }

I don't have to say the advantages above.

Automatic Property in CLR

First let's look at what the compiler does with c#3.x before and after it comes out:

As you can see, c#3.x is just based on. NET programming Language, not based on the. NET Framework. Added some necessary code to make the code (for example, the lack of implementation of the property) complete that we might otherwise appear to be incomplete. At run time, the code is exactly the same as the original code. The Automatic Property code has more than two domains <age>k_backingfield and <name>k_backingfield, and their role is: they correspond to two property (age, Name), and is exactly the same as the two Field (age,name) defined in person. The code is as follows:

internal class Employee
{
// Fields
[CompilerGenerated]
private int <Age>k__BackingField;
[CompilerGenerated]
private string <Name>k__BackingField;

// Methods
public Employee(string name, int age);

// Properties
public int Age { get; private set; }
public string Name { get; set; }
}

Quiz for Automatic Property

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.