D1v3
D1v3

Reputation: 101

What kind of Lambda Expression has an input but doesn't use it?

At work I came across this line of code:

String Key = "ThisKey";
Expression<Func<object, object>> test = t => key.ToString();

I understand that 'test' is a variable that holds an expression but what is the point of declaring 't' when it isn't used on the other side of the lambda operator?

Upvotes: 0

Views: 68

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

Because it was defined as Func<object, object>; if defined as Func<obj>, no params would be required. The last type is the return type, and every type before that is a parameter type passed in. Note that when you execute it, it would be done via:

test(<paramhere>);

or

test.invoke(<paramhere>);

So even though it's not used in the expression, depending on the actual context the underlying component doesn't know what may be getting passed in.

Upvotes: 2

Related Questions