c#3.0 new Features (ii)-initializers

Source: Internet
Author: User
Tags new features visual studio

Written in front:

Do not insist on, is I so many years the most failure of the place, starting today to correct, no one has no power to urge, can only rely on their own determination and action to prove .... Today to learn about automatic properties, initializers, or initializers. I know, such content in the Internet early, I do not know whether to put the home page. Put it again and listen to the opinions of our predecessors.

First, automatic properties:

In C # 2.0, we encapsulate a member, which is an automatic property, as the following example:

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

This is a simple way to write the first code and then use Visual Studio's Refactor->encapsulate field feature, which is handy and well known to everyone. and in C # 3.0, we just need to write the following sentence:

public int Dne { get; set; }// Auto-implemented properties

As you can see, this automatic attribute in C # 3.0 makes a great simplification of the attribute writing in C # 2.0. The automatic properties of C # 3.0 do not need to create a private variable, and give this job to the compiler! Of course, scaling is also handy if you need to add some logic to getting accessor get or setting accessor set.

Second, the object initial value set

Object initializers allow you to assign values to any accessible field or property of an object when an object is created without explicitly calling the constructor. Object initializers, which I see in many places, with their names, also called object initializers. Looking at the example, previously, we first defined a class:

public class MyClass
{
  public string Number { get; set; }
  public string Name { get; set; }
  public MyClass()
  {
  }
  public MyClass(string newID)
  {
    Number = newID;
  }
  public MyClass(string newID, string newName)
  {
    Number = newID;
    Name = newName;
  }
}

The new object is then initialized with its properties:

public static void newObject()
{
  MyClass cobject = new MyClass();
  cobject.Name = "yy";
  cobject.Number = "1111";
  MyClass dobject = new MyClass("1111");
  dobject.Name = "yy";
  MyClass eobject = new MyClass("1111","yy");
}

Now, in C # 3.0, using the object initializer, the new object, and the properties that can be accessed, are initialized with the following code:

MyClass nobject = new MyClass{Name = "yy",Number = "1111"};

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.