Question

Based on the documentation here and here, the two factory methods look interchangeable. Are they?

Was it helpful?

Solution

Expression.Parameter() supports ByRef types (i.e. a ref parameter), while Expression.Variable() will throw an exception if given one.

They are otherwise identical, but that's an implementation detail and you shouldn't rely on it:

public static ParameterExpression Parameter(Type type, string name)
{
    bool isByRef = type.IsByRef;
    if (isByRef)
    {
        type = type.GetElementType();
    }
    return ParameterExpression.Make(type, name, isByRef);
}

public static ParameterExpression Variable(Type type, string name)
{
    if (type.IsByRef)
    {
        throw Error.TypeMustNotBeByRef();
    }
    return ParameterExpression.Make(type, name, false);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top