The New operator is used to create new instances of classes and value types. There are three forms of the New operator:
An expression for creating an object (instance of class or value type).
An expression for creating an array instance.
An expression for creating a delegate instance.
An object creation operator is used to create a new instance of a class type or a value type.
object-creation-expression:
New type constructor-specifieropt ( argument-listopt )
constructor-specifier:
. identifier
An expression should include a type of created object, optional constructor name and arguments list. If the constructor name is not given in the expression, the compiler determines a constructor, the number and the type of arguments in which match the number and type of expressions passed in the arguments list. A compilation error occurs if such a constructor cannot be identified.
An array instance creation operator is used to create a new instance of an array type.
array-creation-expression:
New non-array-type [ expression-list ] array-creation-expression-initializeropt
New ( array-type , array-initializer )
array-creation-expression-initializer:
= array-initializer
A delegate instance creation operator is used to create a new instance of a delegate type.
delegate-creation-expression:
New delegate-type ( expression )
The argument of a delegate instance creation operator must be either a method group or a value of a delegate type. If the argument is a method group, it identifies the method and, for instance methods, the object for which to create a delegate. If the argument is a value of a delegate type, it identifies a delegate instance of which to create a copy.
The compile-time processing of a delegate instance creation operator of the New D(E) form where D is a delegate type and E is an expression consists of the following steps:
If E is a method group:
The set of methods identified by E must include exactly one method that is compatible with D, and this method becomes the one to which the newly created delegate refers. If no matching method exists, or if more than one matching method exists, a compilation error occurs. If the selected method is an instance method, the instance expression associated with E becomes the target object of the delegate.
The operator results in a value of type D, namely a newly created delegate that refers to the selected method and target object (instance reference).
If E is a value of a delegate type:
D and E must be compatible. Otherwise, a compilation error occurs.
The operator results in a value of type D, namely a newly created delegate instance that refers to the same invocation list as E.
Otherwise, the delegate instance creation operator is invalid, and a compilation error occurs.
See also: