Func<Classification, string> test1 = c => c.Id = "x";
Func<Classification, string> test2 = c => { return c.Id = "x";};
I've worked with lambda's for nearly a year or so now and fairly reasonable with them, but today I was looking at NBuilder and seen a weird Func that didn't seem to match the examples. I had play anyway and it checks out but I don't understand why the above compiles let alone runs. We are doing an assignment and thus the expression doesn't evaluate to anything, right??? or not
So I thought maybe something I've missed related to lambda, so I tried something else:
[Test]
public void AmIGoingMad()
{
Assert.That(Test(),Is.Null); // not sure what to expect - compile fail?
}
public string Test()
{
string subject = "";
return subject = "Matt";
}
Sure enough AmIGoingMad
fails and "Matt" is actually returned.
Why do we have this behavior? Where is this documented? Is it purely a syntactic shortcut?
I feel like I missed something fundamental in my understanding of lambda or even C#.
Feeling dumb.