As far as I understand, this is a pointer data type, i.e. it is passed in function not object, and the link? Then if I create a List object somewhere. And after that I will delete / add lines to it in several streams - it seems to me that an exception will arise since I'm trying to change the same object from different threads at the same time. Or does it work somehow differently?
1 answer
List <> is indeed a reference data type. The memory for it is allocated on the heap and the link to the address in the heap is transmitted everywhere. Therefore, to duplicate, as you write in the comments, the List itself cannot. But when working with it from multiple streams, there may indeed be an unexpected result. This is how you organize access to the collection and what you will do with it. You can also throw an exception if, for example, by indexing an element that has already been deleted by another thread. If you need a thread-safe collection, then you already have advised Concurrent collection. The alternative is to organize thread safety with your hands, for example through lock (object).
System.Collections.Concurrent.ConcurrentBag<T>this is a thread-safe replacement forSystem.Collections.Generic.List<T>.......... Also, as I wrote above, to work in streams for collections is presented whole list - Alexey Shimansky