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?
Expression.Variable
is used to declare a local variable within a block. Expression.Parameter
is used to declare a parameter for an incoming value.
Now currently C# doesn't allow statement-bodied lambda expressions, but if it did, imagine:
// Not currently valid, admittedly...
Expression<Func<int, int>> foo = x =>
{
int y = DateTime.Now.Hour;
return x + y;
};
If this were valid, the C# compiler would generate code using Expression.Parameter
for x
, and Expression.Variable
for y
.
At least, that's my understanding. It's a real shame that the documentation for the two methods is basically the same :(
See more on this question at Stackoverflow