Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a readonly field in C# thread safe?

Is a readonly field in C# thread safe?

public class Foo
{
  private readonly int _someField;

  public Foo()
  {
    _someField = 0;
  }

  public Foo(int someField)
  {
    _someField = someField;
  }

  public void SomeMethod()
  {
     doSomething(_someField);
  }
}


Have gone through some posts:

  • What are the benefits to marking a field as readonly in C#? - JaredPar suggests that readonly fields once constructed are immutable and hence safe.
  • Readonly Fields and Thread Safety, suggests that there is some risk if constructors do a lot of work.

So, if the readonly field is used as in the code above, and constructors are light, is it thread-safe? What if _someField is a referrence type (e.g. an array of strings)?

like image 360
publicgk Avatar asked Nov 27 '11 14:11

publicgk


People also ask

What is a readonly field?

In a field declaration, readonly indicates that assignment to the field can only occur as part of the declaration or in a constructor in the same class. A readonly field can be assigned and reassigned multiple times within the field declaration and constructor.

Is a read-only variable?

Read-only variables can be used to gather information about the current template, the user who is currently logged in, or other current settings. These variables are read-only and cannot be assigned a value.

What is the purpose of readonly in C#?

The readonly keyword can be used to define a variable or an object as readable only. This means that the variable or object can be assigned a value at the class scope or in a constructor only. You cannot change the value or reassign a value to a readonly variable or object in any other method except the constructor.

What is the purpose of readonly?

Read-only is a file attribute which only allows a user to view a file, restricting any writing to the file. Setting a file to “read-only” will still allow that file to be opened and read; however, changes such as deletions, overwrites, edits or name changes cannot be made.


Video Answer


3 Answers

Yes - your code doesn't expose this within either constructor, so no other code can "see" the object before it's been fully constructed. The .NET memory model (as of .NET 2) includes a write barrier at the end of every constructor (IIRC - search Joe Duffy's blog posts for more details) so there's no risk of another thread seeing a "stale" value, as far as I'm aware.

I'd personally still usually use a property instead, as a way of separating implementation from API, but from a thread-safety point of view it's fine.

like image 194
Jon Skeet Avatar answered Oct 09 '22 00:10

Jon Skeet


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.

like image 27
SLaks Avatar answered Oct 09 '22 00:10

SLaks


Not looking at your example but in general it depends what is readonly applied to, for example if dictionary is declared readonly you can still update keyvalue pairs

like image 1
Rohit Sharma Avatar answered Oct 09 '22 00:10

Rohit Sharma