ListInit Method (NewExpression, Expression[])

Expression.ListInit Method (NewExpression, Expression())

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Creates a ListInitExpression that uses a method named "Add" to add elements to a collection.

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)

'Declaration
Public Shared Function ListInit ( _
	newExpression As NewExpression, _
	ParamArray initializers As Expression() _
) As ListInitExpression

Parameters

newExpression
Type: System.Linq.Expressions.NewExpression
A NewExpression to set the NewExpression property equal to.
initializers
Type: System.Linq.Expressions.Expression ()
An array of Expression objects to use to populate the Initializers collection.

Return Value

Type: System.Linq.Expressions.ListInitExpression
A ListInitExpression that has the NodeType property equal to ListInit and the NewExpression property set to the specified value.

ExceptionCondition
ArgumentNullException

newExpression or initializers is Nothing.

-or-

One or more elements of initializers are Nothing.

ArgumentException

newExpression.Type does not implement IEnumerable.

InvalidOperationException

There is no instance method named "Add" (case insensitive) declared in newExpression.Type or its base type.

-or-

The add method on newExpression.Type or its base type does not take exactly one argument.

-or-

The type represented by the Type property of the first element of initializers is not assignable to the argument type of the add method on newExpression.Type or its base type.

-or-

More than one argument-compatible method named "Add" (case-insensitive) exists on newExpression.Type and/or its base type.

The Type property of newExpression must represent a type that implements IEnumerable.

In order to use this overload of ListInit(NewExpression, Expression()), newExpression.Type or its base type must declare a single method named "Add" (case insensitive) that takes exactly one argument. The type of the argument must be assignable from the type represented by the Type property of the first element of initializers.

The Initializers property of the returned ListInitExpression contains one element of type ElementInit for each element of initializers. The Arguments property of each element of Initializers is a singleton collection that contains the corresponding element of initializers. The AddMethod property of each element of Initializers represents the add method that was discovered on newExpression.Type or its base type.

The Type property of the resulting ListInitExpression is equal to newExpression.Type.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft