Imagine I have an interface IHeapObject
:
interface IHeapObject
{
string Id {get;}
SomeEnum Type {get;}
}
and it's implementation HeapObject
:
class HeapObject : IHeapObject
{
public string Id {get; private set;}
public SomeEnum Type {get; private set;}
public object objectLocker {get; } = new object();
}
Imagine I have a class that has a public IEnumerable<IHeapObject>
Readonly-Property property.
This property points to a private readonly IList<HeapObject>
field. Like this:
class MyClass
{
public IEnumerable<IHeapObject> Foo => _bar;
private readonly IList<HeapObject> _bar;
}
Suppose the way I handle this collection inside of this class is 100% thread-safe. Furthermore, any and all state changes in the list and HeapObject itself happen only within this class. Is the fact that I'm exposing the collection a thread-safety concern?
My assumptions is that I can't guarantee that the IHeapObject
instance will be "up-to-date" when read from outside MyClass
. Meaning, as long as I don't rely on the properties' values for critical processes outside of it, I should be fine. Is this assumption correct?
Secondly, is there a risk of throwing an exception while iterating over the IEnumerable
collection from outside of the class?
public string Id {get; private set;}
twice?