我有这个问题。
public class Foo : object
{
public override bool Equals(obj a, objb)
{
return ((Foo)a).Bar.GetHashCode() == ((Foo)b).Bar.GetHashCode();
}
}
假设我想让 Foo 线程安全。我需要同步调用 GetHashCode() 吗?
最佳答案
摘自System.Object
的文档:
Public static members of this type are thread safe. Instance members are not guaranteed to be thread-safe.
因此,如果您没有覆盖Object.GetHashCode
,则无法保证线程安全。如果您已覆盖,则取决于您的实现。它还取决于您所说的“thread safe”的含义。
关于c# - GetHashCode 是线程安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2234272/