Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Expression.Variable() and Expression.Parameter()?

Both seem to return the same type, and have the same signature.

So what is the difference between them, and when should we use each?

like image 359
shay__ Avatar asked Aug 29 '15 08:08

shay__


People also ask

What is the difference between a variable and parameter?

A variable is the way in which an attribute or quantity is represented. A parameter is normally a constant in an equation describing a model (a simulation used to reproduce behavior of a system).

What is the difference between variable and parameter in SQL?

A variable in SQL is an object that can hold a single data value of a specific type. In contrast, a parameter in SQL is an object that can exchange data between stored procedures and functions.

What is parameter expression?

Parameter(Type, String) Creates a ParameterExpression node that can be used to identify a parameter or a variable in an expression tree. Parameter(Type) Creates a ParameterExpression node that can be used to identify a parameter or a variable in an expression tree.

What is the difference between a parameter and a decision variable in an optimization problem?

There is a clear difference between variables and parameters. A variable represents a model state, and may change during simulation. A parameter is commonly used to describe objects statically. A parameter is normally a constant in a single simulation, and is changed only when you need to adjust your model behavior.


1 Answers

Effectively, there is no difference, apart from the fact that Variable() does not allow ref types. To see that, you can look at the reference source:

public static ParameterExpression Parameter(Type type, string name) {
    ContractUtils.RequiresNotNull(type, "type");

    if (type == typeof(void)) {
        throw Error.ArgumentCannotBeOfTypeVoid();
    }

    bool byref = type.IsByRef;
    if (byref) {
        type = type.GetElementType();
    }

    return ParameterExpression.Make(type, name, byref);
}

public static ParameterExpression Variable(Type type, string name) {
    ContractUtils.RequiresNotNull(type, "type");
    if (type == typeof(void)) throw Error.ArgumentCannotBeOfTypeVoid();
    if (type.IsByRef) throw Error.TypeMustNotBeByRef();
    return ParameterExpression.Make(type, name, false);
}

As you can see, both methods call ParameterExpression.Make(), so the returned object will behave the same.

like image 121
svick Avatar answered Sep 23 '22 06:09

svick