New characteristics of c#3.0 (III.)-Details automatic properties and anonymous types

Source: Internet
Author: User
Tags anonymous

Written in front:

Do not know how, on a said to the home page to see, today to see only found no put up, do things are really not careful, change, change, change! In fact, I know that C # 4.0 have, but even C # 3.0 have not figured out, use or adhere to this series, and then write C # 4.0 bar. Technical predecessors must have known all these things, but, I also just learn, just to share my study with you!

I. Automatically implemented properties

The full name should be called the Automatically implemented attribute (auto-implemented properties), in the previous article, given the simple example that when no other logic is needed in the property accessor, the automatically implemented property makes the property declaration more concise. As in the preceding example, in C # 2.0:

private int m_one;
public int One
  get { return m_one; }
  set { m_one = value; }

This property has only the save (set) logic, and there is no other logic, such as dynamic assignment or conditional access, in C # 3.0, which can be written as:

public int One { get; set; }

Thus, you do not need to create a private variable that corresponds to this property.

An automatically implemented property must declare both a get and a set accessor. To create a readonly automatic implementation property, give it a private set accessor. Such as:

public string Name { get; private set; }

In this way, this property is read-only and cannot be stored operation. That someone is sure to ask, what to do if you want to assign a value to the name. Carefully understand the meaning of this private set, in the object-oriented concept, private representation within the class is still accessible, that is, the read-only attribute, which is defined in the class itself, can also be accessed, as follows:

public class EDClass
  public string Name { get; private set; }
  public EDClass()
    Name = "qq";
  public void setName()
    Name = "qq";

This is no problem at all, however, if you pass obj. Name = value is definitely not good, otherwise it is not called ReadOnly. For example:

EDClass cls = new EDClass();
cls.Name = "qq";

Error: The property or indexer ' CSharpStudy.EDClass.Name ' cannot to used in this context because the set accessor is Inaccessi ble

It is also important to note that an automatically implemented property is not allowed to have attributes. If you must use attributes on the backing field of a property, you should create only general properties. Good around the mouth, careful analysis of these concepts, in the object-oriented concept, Class members (member) also known as attributes, in fact, in general, in C + + is called a member variable, in C # called attributes, or fields. And here the automatic properties are in the property, just imagine, define an automatic property:

public string Name { get; set; }

You want to define a private member (attributes, attribute) associated with it:

private string _name;

No error, but definitely not associated, it is only considered a class of private member variables.

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: 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.