Expression.ListInit Method

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Creates a ListInitExpression.

Overloads

ListInit(NewExpression, IEnumerable<ElementInit>)

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

ListInit(NewExpression, IEnumerable<Expression>)

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

ListInit(NewExpression, ElementInit[])

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

ListInit(NewExpression, Expression[])

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

ListInit(NewExpression, MethodInfo, IEnumerable<Expression>)

Creates a ListInitExpression that uses a specified method to add elements to a collection.

ListInit(NewExpression, MethodInfo, Expression[])

Creates a ListInitExpression that uses a specified method to add elements to a collection.

ListInit(NewExpression, IEnumerable<ElementInit>)

Source:
ListInitExpression.cs
Source:
ListInitExpression.cs
Source:
ListInitExpression.cs

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

C#
public static System.Linq.Expressions.ListInitExpression ListInit(System.Linq.Expressions.NewExpression newExpression, System.Collections.Generic.IEnumerable<System.Linq.Expressions.ElementInit> initializers);

Parameters

newExpression
NewExpression

A NewExpression to set the NewExpression property equal to.

initializers
IEnumerable<ElementInit>

An IEnumerable<T> that contains ElementInit objects to use to populate the Initializers collection.

Returns

A ListInitExpression that has the NodeType property equal to ListInit and the NewExpression and Initializers properties set to the specified values.

Exceptions

newExpression or initializers is null.

-or-

One or more elements of initializers are null.

newExpression.Type does not implement IEnumerable.

Examples

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

C#
string tree1 = "maple";
string tree2 = "oak";

System.Reflection.MethodInfo addMethod = typeof(Dictionary<int, string>).GetMethod("Add");

// Create two ElementInit objects that represent the
// two key-value pairs to add to the Dictionary.
System.Linq.Expressions.ElementInit elementInit1 =
    System.Linq.Expressions.Expression.ElementInit(
        addMethod,
        System.Linq.Expressions.Expression.Constant(tree1.Length),
        System.Linq.Expressions.Expression.Constant(tree1));
System.Linq.Expressions.ElementInit elementInit2 =
    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<int, string>.
System.Linq.Expressions.NewExpression newDictionaryExpression =
    System.Linq.Expressions.Expression.New(typeof(Dictionary<int, string>));

// Create a ListInitExpression that represents initializing
// a new Dictionary<> instance with two key-value pairs.
System.Linq.Expressions.ListInitExpression 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")}

Remarks

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

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ListInit(NewExpression, IEnumerable<Expression>)

Source:
ListInitExpression.cs
Source:
ListInitExpression.cs
Source:
ListInitExpression.cs

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

C#
public static System.Linq.Expressions.ListInitExpression ListInit(System.Linq.Expressions.NewExpression newExpression, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> initializers);

Parameters

newExpression
NewExpression

A NewExpression to set the NewExpression property equal to.

initializers
IEnumerable<Expression>

An IEnumerable<T> that contains Expression objects to use to populate the Initializers collection.

Returns

A ListInitExpression that has the NodeType property equal to ListInit and the NewExpression property set to the specified value.

Exceptions

newExpression or initializers is null.

-or-

One or more elements of initializers are null.

newExpression.Type does not implement IEnumerable.

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.

Remarks

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

In order to use this overload of ListInit(NewExpression, IEnumerable<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.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ListInit(NewExpression, ElementInit[])

Source:
ListInitExpression.cs
Source:
ListInitExpression.cs
Source:
ListInitExpression.cs

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

C#
public static System.Linq.Expressions.ListInitExpression ListInit(System.Linq.Expressions.NewExpression newExpression, params System.Linq.Expressions.ElementInit[] initializers);

Parameters

newExpression
NewExpression

A NewExpression to set the NewExpression property equal to.

initializers
ElementInit[]

An array of ElementInit objects to use to populate the Initializers collection.

Returns

A ListInitExpression that has the NodeType property equal to ListInit and the NewExpression and Initializers properties set to the specified values.

Exceptions

newExpression or initializers is null.

-or-

One or more elements of initializers are null.

newExpression.Type does not implement IEnumerable.

Examples

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

C#
string tree1 = "maple";
string tree2 = "oak";

System.Reflection.MethodInfo addMethod = typeof(Dictionary<int, string>).GetMethod("Add");

// Create two ElementInit objects that represent the
// two key-value pairs to add to the Dictionary.
System.Linq.Expressions.ElementInit elementInit1 =
    System.Linq.Expressions.Expression.ElementInit(
        addMethod,
        System.Linq.Expressions.Expression.Constant(tree1.Length),
        System.Linq.Expressions.Expression.Constant(tree1));
System.Linq.Expressions.ElementInit elementInit2 =
    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<int, string>.
System.Linq.Expressions.NewExpression newDictionaryExpression =
    System.Linq.Expressions.Expression.New(typeof(Dictionary<int, string>));

// Create a ListInitExpression that represents initializing
// a new Dictionary<> instance with two key-value pairs.
System.Linq.Expressions.ListInitExpression 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")}

Remarks

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

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ListInit(NewExpression, Expression[])

Source:
ListInitExpression.cs
Source:
ListInitExpression.cs
Source:
ListInitExpression.cs

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

C#
public static System.Linq.Expressions.ListInitExpression ListInit(System.Linq.Expressions.NewExpression newExpression, params System.Linq.Expressions.Expression[] initializers);

Parameters

newExpression
NewExpression

A NewExpression to set the NewExpression property equal to.

initializers
Expression[]

An array of Expression objects to use to populate the Initializers collection.

Returns

A ListInitExpression that has the NodeType property equal to ListInit and the NewExpression property set to the specified value.

Exceptions

newExpression or initializers is null.

-or-

One or more elements of initializers are null.

newExpression.Type does not implement IEnumerable.

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.

Remarks

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.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ListInit(NewExpression, MethodInfo, IEnumerable<Expression>)

Source:
ListInitExpression.cs
Source:
ListInitExpression.cs
Source:
ListInitExpression.cs

Creates a ListInitExpression that uses a specified method to add elements to a collection.

C#
public static System.Linq.Expressions.ListInitExpression ListInit(System.Linq.Expressions.NewExpression newExpression, System.Reflection.MethodInfo addMethod, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> initializers);
C#
public static System.Linq.Expressions.ListInitExpression ListInit(System.Linq.Expressions.NewExpression newExpression, System.Reflection.MethodInfo? addMethod, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> initializers);

Parameters

newExpression
NewExpression

A NewExpression to set the NewExpression property equal to.

addMethod
MethodInfo

A MethodInfo that represents an instance method named "Add" (case insensitive), that adds an element to a collection.

initializers
IEnumerable<Expression>

An IEnumerable<T> that contains Expression objects to use to populate the Initializers collection.

Returns

A ListInitExpression that has the NodeType property equal to ListInit and the NewExpression property set to the specified value.

Exceptions

newExpression or initializers is null.

-or-

One or more elements of initializers are null.

newExpression.Type does not implement IEnumerable.

-or-

addMethod is not null and it does not represent an instance method named "Add" (case insensitive) that takes exactly one argument.

-or-

addMethod is not null and the type represented by the Type property of one or more elements of initializers is not assignable to the argument type of the method that addMethod represents.

addMethod is null and no instance method named "Add" that takes one type-compatible argument exists on newExpression.Type or its base type.

Remarks

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

If addMethod is null, newExpression.Type or its base type must declare a single method named "Add" (case insensitive) that takes exactly one argument. If addMethod is not null, it must represent an instance method named "Add" (case insensitive) that has exactly one parameter. The type represented by the Type property of each element of initializers must be assignable to the argument type of the add method.

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 is equal to addMethod.

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ListInit(NewExpression, MethodInfo, Expression[])

Source:
ListInitExpression.cs
Source:
ListInitExpression.cs
Source:
ListInitExpression.cs

Creates a ListInitExpression that uses a specified method to add elements to a collection.

C#
public static System.Linq.Expressions.ListInitExpression ListInit(System.Linq.Expressions.NewExpression newExpression, System.Reflection.MethodInfo addMethod, params System.Linq.Expressions.Expression[] initializers);
C#
public static System.Linq.Expressions.ListInitExpression ListInit(System.Linq.Expressions.NewExpression newExpression, System.Reflection.MethodInfo? addMethod, params System.Linq.Expressions.Expression[] initializers);

Parameters

newExpression
NewExpression

A NewExpression to set the NewExpression property equal to.

addMethod
MethodInfo

A MethodInfo that represents an instance method that takes one argument, that adds an element to a collection.

initializers
Expression[]

An array of Expression objects to use to populate the Initializers collection.

Returns

A ListInitExpression that has the NodeType property equal to ListInit and the NewExpression property set to the specified value.

Exceptions

newExpression or initializers is null.

-or-

One or more elements of initializers are null.

newExpression.Type does not implement IEnumerable.

-or-

addMethod is not null and it does not represent an instance method named "Add" (case insensitive) that takes exactly one argument.

-or-

addMethod is not null and the type represented by the Type property of one or more elements of initializers is not assignable to the argument type of the method that addMethod represents.

addMethod is null and no instance method named "Add" that takes one type-compatible argument exists on newExpression.Type or its base type.

Remarks

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

If addMethod is null, newExpression.Type or its base type must declare a single method named "Add" (case insensitive) that takes exactly one argument. If addMethod is not null, it must represent an instance method named "Add" (case insensitive) that has exactly one parameter. The type represented by the Type property of each element of initializers must be assignable to the argument type of the add method.

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 is equal to addMethod.

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0