Is it thread safe to get an element by index from List?
var list = new List<int>();
...
var a = list[i]; //executed from multiple threads. Should this be protected?
It is absolutely thread safe... ...as long as you can make sure that the list is not changed. Even adding items puts you into "not defined" territoty (although depending on implementation it may just work).
So, read only - yes. Read + manipulation: no.
Note: it is ok to change items IN the lsit as long as the list[5] for example is the same object. The list does not care. Not saying it will work - because depending what you would change here that would be your contract with the objects in the list. For ints you obviously should not update them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With