0

I have this object that many insrances using from different threads:

Private static var _selectedIntrerface;

public static Interfaces SelectedIntrerface
{
    get { return _selectedIntrerface; }
    set { _selectedIntrerface= value; }
}

So my question is very simple: should i declare it this way:

Private static volatile var _selectedIntrerface;

public static volatile Interfaces SelectedIntrerface
{
    get { return _selectedIntrerface; }
    set { _selectedIntrerface= value; }
}
  • Update

This object not modified, only accessed.

david hol
  • 1,272
  • 7
  • 22
  • 44
  • See also https://stackoverflow.com/q/1330590 – Peter Duniho Nov 20 '15 at 08:50
  • What is your motivation for declaring the field volatile if the object never gets modified? – Mathias R. Jessen Nov 20 '15 at 08:53
  • Making it volatile will probably not help much. Unless you're careful you'll still have some horrible race conditions if you allow threads to change the reference willy-nilly. For example, if you allow threads to set the property to null, you're likely to see `NullReferenceException` unless you're careful to always make a copy of the property before checking it for null before dereferencing it. – Matthew Watson Nov 20 '15 at 08:54
  • @MathiasR.Jessen I took him to mean that the object itself is not modified, rather than a new reference never being assigned. – Matthew Watson Nov 20 '15 at 08:55

0 Answers0