I want to have a generic thread safe collection and I saw that the Arraylist can easily be used thread safe by its static Synchronized method but what bugs me is that this ArrayList is not generic so when I want to use my objects I always have to cast them. Is there an easier way to do this? Also other list types would be possible.
C# – Threadsafe and generic arraylist
ccollectionsmultithreadingnetthread-safety
Related Topic
- C# – Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
- C# – How to create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office
- Java – When to use LinkedList over ArrayList in Java
- C# – LINQ’s Distinct() on a particular property
- Java – Initialization of an ArrayList in one line
- Scala: list.flatten: no implicit argument matching parameter type (Any) = > Iterable[Any] was found
Best Answer
A little knowledge is a dangerous thing ;-) Yes, you could use Meta-Knight's suggestion and use
SyncRoot
, but you need to be careful - it's not a panacea. See this post from the BCL Team Blog from Microsoft to see why it's not as simple asSyncRoot
. Quotes from that post:and
Of course, that's not to say it can't be used under any circumstances - you just have to think the use cases through carefully and not be on auto-pilot.