Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are factory methods Expression.Parameter() and Expression.Variable() interchangeable?

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

like image 489
MrCodeMnky Avatar asked Feb 20 '14 22:02

MrCodeMnky


1 Answers

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);
}
like image 66
Cory Nelson Avatar answered Sep 28 '22 00:09

Cory Nelson