Using == or Equals for string comparison

后端 未结 5 1094
抹茶落季
抹茶落季 2020-12-14 23:48

In some languages (e.g. C++) you can\'t use operators like == for string comparisons as that would compare the address of the string object, and not the string itself. Howev

5条回答
  •  执念已碎
    2020-12-15 00:29

    I wouldn't use:

    aa.Equals(bb)
    

    unless I knew aa couldn't possibly be null. I might use:

    string.Equals(aa,bb)
    

    But I'd mainly use that it I wanted to use one of the specific StringComparison modes (invariant, ordinal, case-insensitive, etc). Although I might also use the StringComparer implementations, since they are a bit easier to abstract (for example, to pass into a Dictionary for a case-insensitive ordinal dictionary). For general purpose usage,

    a == b
    

    is fine.

提交回复
热议问题