NewArrayExpression Class

Represents creating a new array and possibly initializing the elements of the new array.

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

public sealed class NewArrayExpression : Expression

The following table shows the different factory methods that you can use to create a NewArrayExpression depending on the NodeType you require.

The following example creates a NewArrayExpression object that represents creating and initializing a one-dimensional array of strings.

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"}

The next example creates a NewArrayExpression object that represents creating a two-dimensional array of strings.

// Create an expression tree that represents creating a  
// two-dimensional array of type string with bounds [3,2].
System.Linq.Expressions.NewArrayExpression newArrayExpression =
    System.Linq.Expressions.Expression.NewArrayBounds(
            typeof(string),
            System.Linq.Expressions.Expression.Constant(3),
            System.Linq.Expressions.Expression.Constant(2));

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

// This code produces the following output: 
// 
// new System.String[,](3, 2)

System.Object
  System.Linq.Expressions.Expression
    System.Linq.Expressions.NewArrayExpression

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
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft