title: C# Concurrent List Questions
body: <p>I have a situation in C# where I have a list of simple types. This list can be accessed by multiple threads: entries can be added or removed, and the existence of an entry can be checked.  I have encapsulated the list in an object exposing just those three operations so far.</p>
  
  <p>I have a few cases to handle (not exactly the same as the methods I just mentioned).<br></p>
  
  <ol>
  <li>A thread can just check for the existence of an entry. (simple)<br></li>
  <li>A thread can check for the existence of an entry, and if it doesn't exist, add it.<br></li>
  <li>A thread needs to check whether an entry exists, and if it does, wait until it is removed.<br></li>
  <li>A combination of 2 and 3, where a thread checks for the existence of an entry, if it does exist, it must wait until it is removed before it can then add it itself.</li>
  </ol>
  
  <p>The whole idea is that the existence of an entry signifies a lock.  If an entry exists, the object it identifies cannot be changed and code cannot proceed because it is being modified elsewhere.</p>
  
  <p>These may seem like simple novice situations but I'm refreshing myself on concurrency issues and it's making me a bit paranoid, and I'm also not as familiar with C#'s concurrency mechanisms.</p>
  
  <p>What would be the best way to handle this?  Am I totally off? Should check and add (test and set?) be combined into a fourth atomic operation?  Would I simply be adding lock blocks to my methods where the list is accessed?</p>
  
  <p>Also, is it possible to unit test this kind of thing (not the simple operations, the concurrency situations)?</p>