This documentation is archived and is not being maintained.

ListInitExpression Class

Represents a constructor call that has a collection initializer.

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

'Declaration
Public NotInheritable Class ListInitExpression _
	Inherits Expression
'Usage
Dim instance As ListInitExpression

Use the ListInit factory methods to create a ListInitExpression.

The value of the NodeType property of a ListInitExpression is ListInit.

The following example creates a ListInitExpression that represents the initialization of a new dictionary instance that has 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), _
        System.Linq.Expressions.Expression.Constant(tree1))
Dim elementInit2 As System.Linq.Expressions.ElementInit = _
    System.Linq.Expressions.Expression.ElementInit( _
        addMethod, _
        System.Linq.Expressions.Expression.Constant(tree2.Length), _
        System.Linq.Expressions.Expression.Constant(tree2))

' 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, _
        elementInit2)

Console.WriteLine(listInitExpression.ToString())

' This code produces the following output: 

' new Dictionary`2() {Void Add(Int32, System.String)(5,"maple"), 
' Void Add(Int32, System.String)(3,"oak")

System.Object
  System.Linq.Expressions.Expression
    System.Linq.Expressions.ListInitExpression

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5
Show: