Is a readonly field in C# thread safe?

后端 未结 3 1312
小蘑菇
小蘑菇 2020-12-17 08:59

Is a readonly field in C# thread safe?

public class Foo
{
  private readonly int _someField;

  public Foo()
  {
    _someField = 0;
  }

  publ         


        
3条回答
  •  别那么骄傲
    2020-12-17 09:25

    That depends what's in the field.

    Reading from a readonly field, or from any field that is smaller than the word length (including all reference types) is an atomic operation.

    However, the object inside the readonly field may or may not be thread-safe.

提交回复
热议问题