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

StackOverflow https://stackoverflow.com/questions/21921011

  •  14-10-2022
  •  | 
  •  

Frage

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

War es hilfreich?

Lösung

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);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top