10.5. The == operator

The == operator in C# deserves some attention from Java developers. It works in exactly the same way the == operator works in Java except for one case – when the operands are string objects.

The == operator behaves differently depending on what its operands are:

  • when used to check the equality of two value types, [8] the operator returns true if both operands have the same value – this is the same for Java;

    [8] Simple types (such as int, long, char, etc.) are value types.

  • when used to check the equality of two reference types, the operator returns true only if the types refer to the same object in memory – this is the same for Java too.

The program below demonstrates these points.

 1: using System; 2: 3: class TestClass { ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.