Alex Rivera | Logout

Is List<T>.Contains() a Threadsafe call - C#

Asked 2009-04-10T18:25:25.637
12

My understanding is that if you are using a generic list (List) in C#, that it can support multiple concurrent readers but only one writer. And when you introduce a writer into the mix, you must also provide synchronization constructs to make the operations thread safe.

Is List.Contains considered a read operation? In other words, if I am calling this method, do I need to worry that a writer may be writing to this List at the same time?

Edit
Report

2 Answers

1

According to the doc ...

Enumerating through a collection is intrinsically not a thread-safe procedure.

So, I would say no it's not thread-safe. You should lock it.

answered 2009-04-10T18:29:49.793
0

It's considered a read operation. You won't run into any race conditions but if you're concerned about getting the latest you could make the List volatile.

answered 2009-04-10T18:32:44.763

Your Answer