Object and collection initializers for new features of c#3.0 language

Source: Internet
Author: User
Tags constructor

In c#3.0, an object-creation expression can contain an object or collection initializer that initializes a member of a newly created object or an element of a newly created collection.

Object-creation expression:

New Type (argument-list (optional)) object or collection initialization (optional)

New type Object or collection Profiler

An object-creation expression can omit a constructor argument list and replace it with parentheses as an object or collection initializer. Omitting a constructor argument list and replacing it with parentheses as an object or collection initializer is equivalent to specifying an empty argument list.

When an object creation expression with an object or collection initializer is executed, the instance constructor is invoked first, and then the member or element initialization specified by the object or collection initializer is executed. An object or collection initializer cannot reference an object instance that is being initialized.

20.4.1 Introducing Object Initializers

In traditional C #, we often use two methods to initialize a class (or struct). One is a constructor with parameters, the other is not a constructor, or the constructor does not actually assign the field, but after the instance of the class is applied, the public property is manually assigned. The following is a geometric point example of a two-dimensional space:

public class Point

    {

        private int xPos, yPos;

        //缺省的构造函数

        public Point()

        {

        }

        public Point(int x, int y)

        {

            xPos = x;

            yPos = y;

        }

        public int X

        {

            get { return xPos; }

            set { xPos = value; }

        }

        public int Y

        {

            get { return yPos; }

            set { yPos = value; }

        }

        public override string ToString()

        {

            return string.Format("[{0}, {1}]", xPos, yPos);

        }

    }

For this class, in the normal way, we'll initialize it like this:

//调用自定义的构造函数

Point p = new Point(100,200);

  //或者手动指定每个属性

  Point p1 = new Point();

  p1.X = 100;

  p1.Y = 200;

Now we take the C # 3.0 code of the class initializer and we can write the following:

var p1 = new Point { X = 100, Y = 200 };

Point p = new Point { X = 100, Y = 200 };

The first of these is an implicit type variable. There is no explicit call to the constructor of point, simply to set the value to the common X and Y properties. Here, the default constructor for the type is invoked, immediately following the assignment of the value to the specified property. At this point, the last two examples are actually simplified formulations of the first instance.

From the example above, we can see that:

The L object initializer consists of a series of member initializers, surrounded by the {and} tokens, separated by commas. Each member initializer begins with the name of an accessible field or property of an object followed by an equal sign, followed by an expression or an object or collection initializer. An error occurs if the object initialization includes more than one member initializer for the same domain or property.

L The member initializer for the expression specified after the equal sign is handled in accordance with the assignment of the domain and property.

L The member initializer of the object initializer is also initialized to a nested object after the equal sign. Unlike assigning a new value to a field or property, an assignment in an object initializer is considered to be assigned to a member of a field or property. A property with a value type cannot be initialized through this construct.

L The member initializer of the collection initializer is specified after the equals sign also initializes a nested collection. Unlike assigning a new collection to a field or property, the element given in the initializer is added to the collection referenced by the field or property. The field or property must be a collection type that meets the requirements specified in the next section.

The L object first timer uses the compiler to assign values to externally visible fields or properties in an object, and the constructor is either compiled or implicitly called, and the assignment to a field or property can be one or more.

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.