Are lambda value objects?
As I understand 2 lambdas should be equal as long as they declare the same code in the same order with the same parameters.
But a simple test on GetHashcode fails:
$
private class LambdaTest
{
private bool x;
public Expression<Func<object, bool>> Predicate
{
get { return o => x; }
}
public LambdaTest(bool x)
{
开发者_运维问答 this.x= x;
}
public override int GetHashCode()
{
return Predicate.GetHashCode();
}
}
$
Test
$
[Test]
public void hashonlambdas()
{
NullSpecification n1 = new NullSpecification(true);
NullSpecification n2 = new NullSpecification(true);
Assert.AreEqual(n1.GetHashCode(), n2.GetHashCode());
}
$
Can you tell me if Im doing something wrong or maybe lambdas are not value objects
I think it's because lambdas are delegates and Delgate is a Reference Type. You can have two delegates of the same type but with distinct invocation list, and they are not the same. In case of lambda, even if they do the same work, if you call one you are not calling the other.
精彩评论