C# difference between == and Equals()

前端 未结 17 1367
走了就别回头了
走了就别回头了 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:42

    Firstly, there is a difference. For numbers

    > 2 == 2.0
    True
    
    > 2.Equals(2.0)
    False
    

    And for strings

    > string x = null;
    > x == null
    True
    
    > x.Equals(null)
    NullReferenceException
    

    In both cases, == behaves more usefully than .Equals

提交回复
热议问题