I have a custom class that implements ICollection
, and this class is readonly, ie. IsReadOnly
returns true (as opposed to using the readonly
keyword), and all functions that would normally modify the data in the collection throw InvalidOperationException
's.
Now, given such a construct, and a quick skim over the thread-safety issues when implementing ICollection
(specifically ICollection.IsSynchronized
and friends), I came up with this quick and dirty solution.
bool ICollection.IsSynchronised { get{ return true; } }
object ICollection.SyncRoot { get{ return new Object(); } }
Now, given the examples in MSDN, this won't cause different threads to lock properly, because they are getting different objects from SyncRoot
. Given that this is a readonly collection though, is this an issue? Are there memory/GC issues with returning new Object()
? Any other issues you can see with this implementation?