1
var list = new List<string>();
var word = "some word";
list.Add("some word");
list.Add("some sentence");
list.Add(word);

I know Equals compare the value stored in the reference while "==" compares whether two references point to the same object. But is it the same even in lambda expressions?

list.RemoveAll(x => x.Equals("some word"));
list.RemoveAll(x => x == "some word");

Do these statements produces same result?

7
  • LINQ does not change how the == operator or Equals method work. Also, == does not necessarily "compare the reference" in C# as it does in Java - it is whatever sensible operator overload has been created. One difference that can bite fairly easily is null.Equals(x) will throw an exception whereas null == x will not. Commented Jan 5, 2017 at 7:12
  • 2
    ""==" compares the reference itself" - that depends on the type of x. If x is a string, it's going to call the == operator in string, which performs a value comparison. If the type of x is object (compile-time type, that is) then it will perform a reference comparison. Commented Jan 5, 2017 at 7:14
  • 2
    Please provide a minimal reproducible example so we can actually reason about the meaning of ==. Ideally, tell us the motivation for the question, too... fundamentally, code in lambda expressions behaves as normal, assuming it's being converted to a delegate. (If it's being converted to an expression tree then the meaning should be the same, but it could be interpreted differently by whatever is consuming the tree.) Commented Jan 5, 2017 at 7:14
  • .Equals does not "compare the value stored in the reference", it does whatever the programmer of T.Equals(T) decided it should do, and in the context of strings it does a string value comparison, not a reference comparison, though surely two equal references means equal values as well, but even with different references, but the same value, .Equals on string will return true. Commented Jan 5, 2017 at 7:19
  • @Lasse V.Karlsen Yeah I agree that. I wanted to know incase of String objects alone. Commented Jan 5, 2017 at 7:21

2 Answers 2

5

I know Equals compare the value stored in the reference while "==" compares whether two references point to the same object.

No, that's just the behaviour of the default == operator for reference types. When both sides of == have a compile-time type of string, the bool operator==(string, string) overload declared in the string class is used, and you get the same result as Equals, when the target of the call isn't null.

That behaviour is exactly the same in regular code as in lambda expressions. Code written in a lambda expression should behave exactly the same way as code written not in a lambda expression... although if the lambda expression is converted to an expression tree, the compiler only emits the code as data, so it's up to whatever's consuming the expression tree to do the right thing with it.

So yes, the code should work absolutely fine - the only difference between using Equals and using == is that if you list contains any null elements, the x.Equals(...) call will throw a NullReferenceException. Personally I usually compare strings with == just for simplicity.

Sign up to request clarification or add additional context in comments.

2 Comments

I agree with your answer if the program is written in c#.But in Java there is no operator overloading how does it behave in that case?
@Prabu: Then it will obey the rules of Java instead. There's no point asking a question about one specific language and then assuming the results will apply to every other language.
3

But is it the same even in lambda expressions?

There is a simple way to answer that: Test it!

void Main()
{
    WhatIsThis<string>(x => x.Equals("foo"));
    Console.WriteLine();
    WhatIsThis<string>(x => x == "foo");
}

void WhatIsThis<T>(Expression<Func<T, bool>> expr)
{
    Console.WriteLine(expr.Body.NodeType);
    if (expr.Body is MethodCallExpression)
    {
        var ce = expr.Body as MethodCallExpression;
        Console.WriteLine(ce.Method.Name);
        Console.WriteLine(ce.Object.ToString());
        Console.WriteLine(string.Join(", ", ce.Arguments.Select(x => x.ToString())));
    }
    else if (expr.Body is BinaryExpression)
    {
        var be = expr.Body as BinaryExpression;
        Console.WriteLine(be.Method.ToString());
        Console.WriteLine(be.Left.ToString());
        Console.WriteLine(be.Right.ToString());
    }
}

This produces the following output:

Call
Equals
x
"foo"

Equal
Boolean op_Equality(System.String, System.String)
x
"foo"

So, the answer is no. Even in lambda expressions, the difference between the == operator and the Equals method call is maintained. And if you think about it: Why shouldn’t it be the case? It makes no sense for lambda expressions to assume that they would do the same thing here and just simplify it to one or another type. After all, there is a difference.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.