Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我运行以下代码 10000 次(a 和 b 都是 C 类的实例)
1.ReferenceEquals(a,b) 0.1 ms 2.a==b 0.7 ms 3.a.Equals(b) 0.8 ms
你能告诉我为什么 1 比 2 和 3 快吗...
没有看到任何代码很难说,但是:
这可能是因为有问题的类已经实现了==运算符,因此它不比较引用。相反,它可能是以某种方式比较对象的内容,这当然会慢得多。
==
(string例如,班级会这样做。)
string