IndexExpression Class
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.
Represents indexing a property or array.
public ref class IndexExpression sealed : System::Linq::Expressions::Expression, System::Linq::Expressions::IArgumentProvider
public ref class IndexExpression sealed : System::Linq::Expressions::Expression
public sealed class IndexExpression : System.Linq.Expressions.Expression, System.Linq.Expressions.IArgumentProvider
public sealed class IndexExpression : System.Linq.Expressions.Expression
type IndexExpression = class
inherit Expression
interface IArgumentProvider
type IndexExpression = class
inherit Expression
Public NotInheritable Class IndexExpression
Inherits Expression
Implements IArgumentProvider
Public NotInheritable Class IndexExpression
Inherits Expression
- Inheritance
- Implements
The following code example shows how to create an object of the IndexExpression type and use it to change a value of an array element by using the ArrayAccess method.
// Add the following directive to your file:
// using System.Linq.Expressions;
// This parameter expression represents a variable that will hold the array.
ParameterExpression arrayExpr = Expression.Parameter(typeof(int[]), "Array");
// This parameter expression represents an array index.
ParameterExpression indexExpr = Expression.Parameter(typeof(int), "Index");
// This parameter represents the value that will be added to a corresponding array element.
ParameterExpression valueExpr = Expression.Parameter(typeof(int), "Value");
// This expression represents an array access operation.
// It can be used for assigning to, or reading from, an array element.
Expression arrayAccessExpr = Expression.ArrayAccess(
arrayExpr,
indexExpr
);
// This lambda expression assigns a value provided to it to a specified array element.
// The array, the index of the array element, and the value to be added to the element
// are parameters of the lambda expression.
Expression<Func<int[], int, int, int>> lambdaExpr = Expression.Lambda<Func<int[], int, int, int>>(
Expression.Assign(arrayAccessExpr, Expression.Add(arrayAccessExpr, valueExpr)),
arrayExpr,
indexExpr,
valueExpr
);
// Print out expressions.
Console.WriteLine("Array Access Expression:");
Console.WriteLine(arrayAccessExpr.ToString());
Console.WriteLine("Lambda Expression:");
Console.WriteLine(lambdaExpr.ToString());
Console.WriteLine("The result of executing the lambda expression:");
// The following statement first creates an expression tree,
// then compiles it, and then executes it.
// Parameters passed to the Invoke method are passed to the lambda expression.
Console.WriteLine(lambdaExpr.Compile().Invoke(new int[] { 10, 20, 30 }, 0, 5));
// This code example produces the following output:
//
// Array Access Expression:
// Array[Index]
// Lambda Expression:
// (Array, Index, Value) => (Array[Index] = (Array[Index] + Value))
// The result of executing the lambda expression:
// 15
' Add the following directive to your file:
' Imports System.Linq.Expressions
' This parameter expression represents a variable that will hold the array.
Dim arrayExpr As ParameterExpression = Expression.Parameter(GetType(Integer()), "Array")
' This parameter expression represents an array index.
' For multidimensional arrays, you can define several indexes.
Dim indexExpr As ParameterExpression = Expression.Parameter(GetType(Integer), "Index")
' This parameter represents the value that will be added to a corresponding array element.
Dim valueExpr As ParameterExpression = Expression.Parameter(GetType(Integer), "Value")
' This expression represents an array access operation.
' It can be used for assigning to, or reading from, an array element.
Dim arrayAccessExpr As Expression = Expression.ArrayAccess(
arrayExpr,
indexExpr
)
' This lambda expression assigns a value provided to it to a specified array element.
' The array, the index of the array element, and the value to be added to the element
' are parameters of the lambda expression.
Dim lambdaExpr As Expression(Of Func(Of Integer(), Integer, Integer, Integer)) =
Expression.Lambda(Of Func(Of Integer(), Integer, Integer, Integer))(
Expression.Assign(arrayAccessExpr, Expression.Add(arrayAccessExpr, valueExpr)),
arrayExpr,
indexExpr,
valueExpr
)
' Print expressions.
Console.WriteLine("Array Access Expression:")
Console.WriteLine(arrayAccessExpr.ToString())
Console.WriteLine("Lambda Expression:")
Console.WriteLine(lambdaExpr.ToString())
Console.WriteLine("The result of executing the lambda expression:")
' The following statement first creates an expression tree,
' then compiles it, and then executes it.
' Parameters passed to the Invoke method are passed to the lambda expression.
Console.WriteLine(lambdaExpr.Compile().Invoke(New Integer() {10, 20, 30}, 0, 5))
' This code example produces the following output:
'
' Array Access Expression:
' Array[Index]
' Lambda Expression:
' (Array, Index, Value) => (Array[Index] = (Array[Index] + Value))
' The result of executing the lambda expression:
' 15
Arguments |
Gets the arguments that will be used to index the property or array. |
Can |
Indicates that the node can be reduced to a simpler node. If this returns true, Reduce() can be called to produce the reduced form. (Inherited from Expression) |
Indexer |
Gets the PropertyInfo for the property if the expression represents an indexed property, returns null otherwise. |
Node |
Returns the node type of this Expression. |
Object |
An object to index. |
Type |
Gets the static type of the expression that this Expression represents. |
Accept(Expression |
Dispatches to the specific visit method for this node type. For example, MethodCallExpression calls the VisitMethodCall(MethodCallExpression). (Inherited from Expression) |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
Get |
Serves as the default hash function. (Inherited from Object) |
Get |
Gets the Type of the current instance. (Inherited from Object) |
Memberwise |
Creates a shallow copy of the current Object. (Inherited from Object) |
Reduce() |
Reduces this node to a simpler expression. If CanReduce returns true, this should return a valid expression. This method can return another node which itself must be reduced. (Inherited from Expression) |
Reduce |
Reduces this node to a simpler expression. If CanReduce returns true, this should return a valid expression. This method can return another node which itself must be reduced. (Inherited from Expression) |
Reduce |
Reduces the expression to a known node type (that is not an Extension node) or just returns the expression if it is already a known type. (Inherited from Expression) |
To |
Returns a textual representation of the Expression. (Inherited from Expression) |
Update(Expression, IEnumerable<Expression>) |
Creates a new expression that is like this one, but using the supplied children. If all of the children are the same, it will return this expression. |
Visit |
Reduces the node and then calls the visitor delegate on the reduced expression. The method throws an exception if the node is not reducible. (Inherited from Expression) |
IArgument |
Returns the number of arguments to the expression tree node. You should not use this member. It is only public due to assembly refactoring, and it is used internally for performance optimizations. |
IArgument |
Returns the argument at index, throwing if index is out of bounds. You should not use this member. It is only public due to assembly refactoring, and it is used internally for performance optimizations. |
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 |
.NET Framework | 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 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: