old-code/monodoc/ecma334/14.5.10.1.xml

9 строки
4.9 KiB
XML

<?xml version="1.0"?>
<clause number="14.5.10.1" title="Object creation expressions">
<paragraph>An <non_terminal where="14.5.10.1">object-creation-expression</non_terminal> is used to create a new instance of a <non_terminal where="11.2">class-type</non_terminal> or a <non_terminal where="11.1">value-type</non_terminal>. <grammar_production><name><non_terminal where="14.5.10.1">object-creation-expression</non_terminal></name> : <rhs><keyword>new</keyword><non_terminal where="11">type</non_terminal><terminal>(</terminal><non_terminal where="14.4.1">argument-list</non_terminal><opt/><terminal>)</terminal></rhs></grammar_production></paragraph>
<paragraph>The type of an <non_terminal where="14.5.10.1">object-creation-expression</non_terminal> must be a <non_terminal where="11.2">class-type</non_terminal> or a <non_terminal where="11.1">value-type</non_terminal>. The type cannot be an abstract <non_terminal where="11.2">class-type</non_terminal>. </paragraph>
<paragraph>The optional <non_terminal where="14.4.1">argument-list</non_terminal> (<hyperlink>14.4.1</hyperlink>) is permitted only if the type is a <non_terminal where="11.2">class-type</non_terminal> or a <non_terminal where="11.1">struct-type</non_terminal>. </paragraph>
<paragraph>The compile-time processing of an <non_terminal where="14.5.10.1">object-creation-expression</non_terminal> of the form new T(A), where T is a <non_terminal where="11.2">class-type</non_terminal> or a <non_terminal where="11.1">value-type</non_terminal> and A is an optional <non_terminal where="14.4.1">argument-list</non_terminal>, consists of the following steps: <list><list_item> If T is a <non_terminal where="11.1">value-type</non_terminal> and A is not present: </list_item><list><list_item> The <non_terminal where="14.5.10.1">object-creation-expression</non_terminal> is a default constructor invocation. The result of the <non_terminal where="14.5.10.1">object-creation-expression</non_terminal> is a value of type T, namely the default value for T as defined in <hyperlink>11.1.1</hyperlink>. </list_item></list><list_item> Otherwise, if T is a <non_terminal where="11.2">class-type</non_terminal> or a struct-type: </list_item><list><list_item> If T is an abstract <non_terminal where="11.2">class-type</non_terminal>, a compile-time error occurs. </list_item><list_item> The instance constructor to invoke is determined using the overload resolution rules of <hyperlink>14.4.2</hyperlink>. The set of candidate instance constructors consists of all accessible instance constructors declared in T. If the set of candidate instance constructors is empty, or if a single best instance constructor cannot be identified, a compile-time error occurs. </list_item><list_item> The result of the <non_terminal where="14.5.10.1">object-creation-expression</non_terminal> is a value of type T, namely the value produced by invoking the instance constructor determined in the step above. </list_item></list><list_item> Otherwise, the <non_terminal where="14.5.10.1">object-creation-expression</non_terminal> is invalid, and a compile-time error occurs. </list_item></list></paragraph>
<paragraph>The run-time processing of an <non_terminal where="14.5.10.1">object-creation-expression</non_terminal> of the form new T(A), where T is <non_terminal where="11.2">class-type</non_terminal> or a <non_terminal where="11.1">struct-type</non_terminal> and A is an optional <non_terminal where="14.4.1">argument-list</non_terminal>, consists of the following steps: <list><list_item> If T is a class-type: </list_item><list><list_item> A new instance of class T is allocated. If there is not enough memory available to allocate the new instance, a System.OutOfMemoryException is thrown and no further steps are executed. </list_item><list_item> All fields of the new instance are initialized to their default values (<hyperlink>12.2</hyperlink>). </list_item><list_item> The instance constructor is invoked according to the rules of function member invocation (<hyperlink>14.4.3</hyperlink>). A reference to the newly allocated instance is automatically passed to the instance constructor and the instance can be accessed from within that constructor as this. </list_item></list><list_item> If T is a struct-type: </list_item><list><list_item> An instance of type T is created by allocating a temporary local variable. Since an instance constructor of a <non_terminal where="11.1">struct-type</non_terminal> is required to definitely assign a value to each field of the instance being created, no initialization of the temporary variable is necessary. </list_item><list_item> The instance constructor is invoked according to the rules of function member invocation (<hyperlink>14.4.3</hyperlink>). A reference to the newly allocated instance is automatically passed to the instance constructor and the instance can be accessed from within that constructor as this. </list_item></list></list></paragraph>
</clause>