Rediger

Del via


Expression.NewArrayInit Method

Definition

Creates a NewArrayExpression that represents creating a one-dimensional array and initializing it from a list of elements.

Overloads

NewArrayInit(Type, IEnumerable<Expression>)

Creates a NewArrayExpression that represents creating a one-dimensional array and initializing it from a list of elements.

NewArrayInit(Type, Expression[])

Creates a NewArrayExpression that represents creating a one-dimensional array and initializing it from a list of elements.

NewArrayInit(Type, IEnumerable<Expression>)

Source:
NewArrayExpression.cs
Source:
NewArrayExpression.cs
Source:
NewArrayExpression.cs

Creates a NewArrayExpression that represents creating a one-dimensional array and initializing it from a list of elements.

public:
 static System::Linq::Expressions::NewArrayExpression ^ NewArrayInit(Type ^ type, System::Collections::Generic::IEnumerable<System::Linq::Expressions::Expression ^> ^ initializers);
public static System.Linq.Expressions.NewArrayExpression NewArrayInit (Type type, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> initializers);
static member NewArrayInit : Type * seq<System.Linq.Expressions.Expression> -> System.Linq.Expressions.NewArrayExpression
Public Shared Function NewArrayInit (type As Type, initializers As IEnumerable(Of Expression)) As NewArrayExpression

Parameters

type
Type

A Type that represents the element type of the array.

initializers
IEnumerable<Expression>

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

Returns

A NewArrayExpression that has the NodeType property equal to NewArrayInit and the Expressions property set to the specified value.

Exceptions

type or initializers is null.

-or-

An element of initializers is null.

The Type property of an element of initializers represents a type that is not assignable to the type that type represents.

Examples

The following example demonstrates how to use the NewArrayInit method to create an expression tree that represents creating a one-dimensional string array that is initialized with a list of string expressions.

List<System.Linq.Expressions.Expression> trees =
    new List<System.Linq.Expressions.Expression>()
        { System.Linq.Expressions.Expression.Constant("oak"),
          System.Linq.Expressions.Expression.Constant("fir"),
          System.Linq.Expressions.Expression.Constant("spruce"),
          System.Linq.Expressions.Expression.Constant("alder") };

// Create an expression tree that represents creating and
// initializing a one-dimensional array of type string.
System.Linq.Expressions.NewArrayExpression newArrayExpression =
    System.Linq.Expressions.Expression.NewArrayInit(typeof(string), trees);

// Output the string representation of the Expression.
Console.WriteLine(newArrayExpression.ToString());

// This code produces the following output:
//
// new [] {"oak", "fir", "spruce", "alder"}
Dim trees As New System.Collections.Generic.List(Of System.Linq.Expressions.Expression) _
        (New System.Linq.Expressions.Expression() _
         {System.Linq.Expressions.Expression.Constant("oak"), _
          System.Linq.Expressions.Expression.Constant("fir"), _
          System.Linq.Expressions.Expression.Constant("spruce"), _
          System.Linq.Expressions.Expression.Constant("alder")})

' Create an expression tree that represents creating and  
' initializing a one-dimensional array of type string.
Dim newArrayExpression As System.Linq.Expressions.NewArrayExpression = _
    System.Linq.Expressions.Expression.NewArrayInit(Type.GetType("System.String"), trees)

' Output the string representation of the Expression.
Console.WriteLine(newArrayExpression.ToString())

' This code produces the following output:
'
' new [] {"oak", "fir", "spruce", "alder"}

Remarks

The Type property of each element of initializers must represent a type that is assignable to the type represented by type, possibly after it is quoted.

Note

An element will be quoted only if type is Expression. Quoting means the element is wrapped in a Quote node. The resulting node is a UnaryExpression whose Operand property is the element of initializers.

The Type property of the resulting NewArrayExpression represents an array type whose rank is 1 and whose element type is type.

Applies to

NewArrayInit(Type, Expression[])

Source:
NewArrayExpression.cs
Source:
NewArrayExpression.cs
Source:
NewArrayExpression.cs

Creates a NewArrayExpression that represents creating a one-dimensional array and initializing it from a list of elements.

public:
 static System::Linq::Expressions::NewArrayExpression ^ NewArrayInit(Type ^ type, ... cli::array <System::Linq::Expressions::Expression ^> ^ initializers);
public static System.Linq.Expressions.NewArrayExpression NewArrayInit (Type type, params System.Linq.Expressions.Expression[] initializers);
static member NewArrayInit : Type * System.Linq.Expressions.Expression[] -> System.Linq.Expressions.NewArrayExpression
Public Shared Function NewArrayInit (type As Type, ParamArray initializers As Expression()) As NewArrayExpression

Parameters

type
Type

A Type that represents the element type of the array.

initializers
Expression[]

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

Returns

A NewArrayExpression that has the NodeType property equal to NewArrayInit and the Expressions property set to the specified value.

Exceptions

type or initializers is null.

-or-

An element of initializers is null.

The Type property of an element of initializers represents a type that is not assignable to the type type.

Examples

The following example demonstrates how to use the NewArrayInit method to create an expression tree that represents creating a one-dimensional string array that is initialized with a list of string expressions.

List<System.Linq.Expressions.Expression> trees =
    new List<System.Linq.Expressions.Expression>()
        { System.Linq.Expressions.Expression.Constant("oak"),
          System.Linq.Expressions.Expression.Constant("fir"),
          System.Linq.Expressions.Expression.Constant("spruce"),
          System.Linq.Expressions.Expression.Constant("alder") };

// Create an expression tree that represents creating and
// initializing a one-dimensional array of type string.
System.Linq.Expressions.NewArrayExpression newArrayExpression =
    System.Linq.Expressions.Expression.NewArrayInit(typeof(string), trees);

// Output the string representation of the Expression.
Console.WriteLine(newArrayExpression.ToString());

// This code produces the following output:
//
// new [] {"oak", "fir", "spruce", "alder"}
Dim trees As New System.Collections.Generic.List(Of System.Linq.Expressions.Expression) _
        (New System.Linq.Expressions.Expression() _
         {System.Linq.Expressions.Expression.Constant("oak"), _
          System.Linq.Expressions.Expression.Constant("fir"), _
          System.Linq.Expressions.Expression.Constant("spruce"), _
          System.Linq.Expressions.Expression.Constant("alder")})

' Create an expression tree that represents creating and  
' initializing a one-dimensional array of type string.
Dim newArrayExpression As System.Linq.Expressions.NewArrayExpression = _
    System.Linq.Expressions.Expression.NewArrayInit(Type.GetType("System.String"), trees)

' Output the string representation of the Expression.
Console.WriteLine(newArrayExpression.ToString())

' This code produces the following output:
'
' new [] {"oak", "fir", "spruce", "alder"}

Remarks

The Type property of each element of initializers must represent a type that is assignable to the type represented by type, possibly after it is quoted.

Note

An element will be quoted only if type is Expression. Quoting means the element is wrapped in a Quote node. The resulting node is a UnaryExpression whose Operand property is the element of initializers.

The Type property of the resulting NewArrayExpression represents an array type whose rank is 1 and whose element type is type.

Applies to