C# difference between == and Equals()

前端 未结 17 1369
走了就别回头了
走了就别回头了 2020-11-21 06:56

I have a condition in a silverlight application that compares 2 strings, for some reason when I use == it returns false while .Equals()

17条回答
  •  感动是毒
    2020-11-21 07:55

    == Operator

    1. If operands are Value Types and their values are equal, it returns true else false.
    2. If operands are Reference Types with exception of string and both refer to the same instance (same object), it returns true else false.
    3. If operands are string type and their values are equal, it returns true else false.

    .Equals

    1. If operands are Reference Types, it performs Reference Equality that is if both refer to the same instance (same object), it returns true else false.
    2. If Operands are Value Types then unlike == operator it checks for their type first and if their types are same it performs == operator else it returns false.

提交回复
热议问题