I was wondering if this statement would cause sync issues:
List<Character> characters = World.CharacterManager.Characters;
‘Characters’ is a class
‘CharacterManager.Characters’ would look something like this:
public List<Character> Characters
{
get
{
lock (this.objLock) { return this.characters; }
}
}
would this cause synchronization problems?
I want to use the referenced List to iterate through to find the character I am looking for.
The lock is useless that way. You will have to use a thread-safe collection such as Will suggested, or if you don’t need write access you can expose only a read only version of your list like so:
These collections cannot be modified, so if your
Charactertype is immutable, you don’t have any synchronization issues. IfCharacteris mutable, you have again a problem, but you would have that problem even with a thread-safe collection. I hope you’re aware of that. You can also expose the property returning anIList<Character>, but usually I find it better to tell the caller that the object is read only.If you need write access, you could also do that by providing the appropriate methods at the scope of the
CharacterManagerand synchronize them. Jesse has written a nice example on how to do this.EDIT: SyncRoot is not present on ICollection<T>.