Expression.ListInit Method (NewExpression, ElementInit())


Creates a ListInitExpression that uses specified ElementInit objects to initialize a collection.

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

Public Shared Function ListInit (
	newExpression As NewExpression,
	ParamArray initializers As ElementInit()
) As ListInitExpression


Type: System.Linq.Expressions.NewExpression

A NewExpression to set the NewExpression property equal to.

Type: System.Linq.Expressions.ElementInit()

An array of ElementInit 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 and Initializers properties set to the specified values.

Exception Condition

newExpression or initializers is null.


One or more elements of initializers are null.


newExpression.Type does not implement IEnumerable.

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

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

The following example demonstrates how to use the ListInit(NewExpression, ElementInit()) method to create a ListInitExpressionthat represents the initialization of a new dictionary instance with two key-value pairs.

Dim tree1 As String = "maple"
Dim tree2 As String = "oak"

Dim addMethod As System.Reflection.MethodInfo = _
    Type.GetType("System.Collections.Generic.Dictionary`2[System.Int32, System.String]").GetMethod("Add")

' Create two ElementInit objects that represent the
' two key-value pairs to add to the Dictionary.
Dim elementInit1 As System.Linq.Expressions.ElementInit = _
    System.Linq.Expressions.Expression.ElementInit( _
        addMethod, _
        System.Linq.Expressions.Expression.Constant(tree1.Length), _
Dim elementInit2 As System.Linq.Expressions.ElementInit = _
    System.Linq.Expressions.Expression.ElementInit( _
        addMethod, _
        System.Linq.Expressions.Expression.Constant(tree2.Length), _

' Create a NewExpression that represents constructing
' a new instance of Dictionary(Of Integer, String).
Dim newDictionaryExpression As System.Linq.Expressions.NewExpression = _
    System.Linq.Expressions.Expression.[New](Type.GetType("System.Collections.Generic.Dictionary`2[System.Int32, System.String]"))

' Create a ListInitExpression that represents initializing
' a new Dictionary(Of T) instance with two key-value pairs.
Dim listInitExpression As System.Linq.Expressions.ListInitExpression = _
    System.Linq.Expressions.Expression.ListInit( _
        newDictionaryExpression, _
        elementInit1, _


' This code produces the following output:
' new Dictionary`2() {Void Add(Int32, System.String)(5,"maple"),
' Void Add(Int32, System.String)(3,"oak")

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top