13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 14 ExpressionsThe optional argument-list (§14.4.1) is permitted only if the type is a class-type or a struct-type.The compile-time processing of an object-creation-expression of the form new T(A), where T is a class-typeor a value-type and A is an optional argument-list, consists of the following steps:• If T is a value-type and A is not present:The object-creation-expression is a default constructor invocation. The result of the object-creationexpressionis a value of type T, namely the default value for T as defined in §11.1.1.• Otherwise, if T is a class-type or a struct-type:If T is an abstract class-type, a compile-time error occurs.The instance constructor to invoke is determined using the overload resolution rules of §14.4.2. The set ofcandidate instance constructors consists of all accessible instance constructors declared in T. If the set ofcandidate instance constructors is empty, or if a single best instance constructor cannot be identified, acompile-time error occurs.The result of the object-creation-expression is a value of type T, namely the value produced by invoking theinstance constructor determined in the step above.• Otherwise, the object-creation-expression is invalid, and a compile-time error occurs.The run-time processing of an object-creation-expression of the form new T(A), where T is class-type or astruct-type and A is an optional argument-list, consists of the following steps:• If T is a class-type: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.All fields of the new instance are initialized to their default values (§12.2).The instance constructor is invoked according to the rules of function member invocation (§14.4.3). Areference to the newly allocated instance is automatically passed to the instance constructor and the instancecan be accessed from within that constructor as this.• If T is a struct-type:An instance of type T is created by allocating a temporary local variable. Since an instance constructor of astruct-type is required to definitely assign a value to each field of the instance being created, no initializationof the temporary variable is necessary.The instance constructor is invoked according to the rules of function member invocation (§14.4.3). Areference to the newly allocated instance is automatically passed to the instance constructor and the instancecan be accessed from within that constructor as this.14.5.10.2 Array creation expressionsAn array-creation-expression is used to create a new instance of an array-type.array-creation-expression:new non-array-type [ expression-list ] rank-specifiers opt array-initializer optnew array-type array-initializerAn array creation expression of the first form allocates an array instance of the type that results fromdeleting each of the individual expressions from the expression list. [Example: For example, the arraycreation expression new int[10,20] produces an array instance of type int[,], and the array creationexpression new int[10][,] produces an array of type int[][,]. end example] Each expression in theexpression list must be of type int, uint, long, or ulong, or of a type that can be implicitly converted toone or more of these types. The value of each expression determines the length of the correspondingdimension in the newly allocated array instance. Since the length of an array dimension must be147

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!