O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 { ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required