Expression.Add Method

Definition

Creates a BinaryExpression that represents an arithmetic addition operation that does not have overflow checking.

Overloads

Add(Expression, Expression)

Creates a BinaryExpression that represents an arithmetic addition operation that does not have overflow checking.

Add(Expression, Expression, MethodInfo)

Creates a BinaryExpression that represents an arithmetic addition operation that does not have overflow checking. The implementing method can be specified.

Add(Expression, Expression)

Source:
BinaryExpression.cs
Source:
BinaryExpression.cs
Source:
BinaryExpression.cs

Creates a BinaryExpression that represents an arithmetic addition operation that does not have overflow checking.

C#
public static System.Linq.Expressions.BinaryExpression Add(System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right);

Parameters

left
Expression

A Expression to set the Left property equal to.

right
Expression

A Expression to set the Right property equal to.

Returns

A BinaryExpression that has the NodeType property equal to Add and the Left and Right properties set to the specified values.

Exceptions

left or right is null.

The addition operator is not defined for left.Type and right.Type.

Examples

The following code example shows how to create an expression that adds two integers.

C#
// Add the following directive to your file:
// using System.Linq.Expressions;

// This expression adds the values of its two arguments.
// Both arguments must be of the same type.
Expression sumExpr = Expression.Add(
    Expression.Constant(1),
    Expression.Constant(2)
);

// Print out the expression.
Console.WriteLine(sumExpr.ToString());

// The following statement first creates an expression tree,
// then compiles it, and then executes it.
Console.WriteLine(Expression.Lambda<Func<int>>(sumExpr).Compile()());

// This code example produces the following output:
//
// (1 + 2)
// 3

Remarks

For more information about this API, see Supplemental API remarks for Expression.Add.

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

Add(Expression, Expression, MethodInfo)

Source:
BinaryExpression.cs
Source:
BinaryExpression.cs
Source:
BinaryExpression.cs

Creates a BinaryExpression that represents an arithmetic addition operation that does not have overflow checking. The implementing method can be specified.

C#
public static System.Linq.Expressions.BinaryExpression Add(System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, System.Reflection.MethodInfo method);
C#
public static System.Linq.Expressions.BinaryExpression Add(System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, System.Reflection.MethodInfo? method);

Parameters

left
Expression

A Expression to set the Left property equal to.

right
Expression

A Expression to set the Right property equal to.

method
MethodInfo

A MethodInfo to set the Method property equal to.

Returns

A BinaryExpression that has the NodeType property equal to Add and the Left, Right and Method properties set to the specified values.

Exceptions

left or right is null.

method is not null and the method it represents returns void, is not static (Shared in Visual Basic), or does not take exactly two arguments.

method is null and the addition operator is not defined for left.Type and right.Type.

Remarks

For more information about this API, see Supplemental API remarks for Expression.Add.

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