(.NET) 之间有什么区别(在 bref 中)
myString == otherString
和
myString.CompareTo(otherString) == 0
最佳答案
没有区别,除非 myString 为 null
,在这种情况下 myString.CompareTo(otherString)
会抛出错误 (NullReferenceException
)。此外,使用 CompareTo
比 ==
慢一点。
仅当您有兴趣了解一个字符串在按字母顺序排序时是在另一个字符串之前还是之后时,才使用 CompareTo
。例如 "Car".CompareTo("Cat")
返回 -1,因为按字母顺序排列时“Car”在“Cat”之前。
关于c# - .NET 中的字符串比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9330573/