title: ConcurrentBag TryTake Method
body: <p>with reference from MSDN <code>ConcurrentBag&lt;T&gt;::TryTake</code> Method 
  Attempts to remove and return an object from the ConcurrentBag.</p>
  
  <p>I am wondering about on which basis it removes object from the Bag, as per my understanding dictionary add and remove works on the basis of HashCode.</p>
  
  <p>If concurrent bag has nothing to do with the hascode, what will happen when the object values get change during the add and remove.</p>
  
  <p>forexample.</p>
  
  <pre><code>public static IDictionary&lt;string, ConcurrentBag&lt;Test&gt;&gt; SereverConnections = new ConcurrentDictionary&lt;string, ConcurrentBag&lt;Test&gt;&gt;();
  
  
   public class Student
      {
         public string FirstName{get;set;}
         Student student=new Student();
         SereverConnections.add(student.FirstName{"bilal"});
  
      }
  </code></pre>
  
  <p>//have change the name from </p>
  
  <pre><code>//student.FirstName="khan"; property of the object has been changed
  </code></pre>
  
  <p>now the object properties values has been changed,</p>
  
  <p>What will be the behavior of when I remove <code>ConcurrentBag&lt;T&gt;::TryTake</code> Method? how it will track the object is same when added?   </p>
  
  <p>Code:</p>
  
  <pre><code> public class Test
      {
          public HashSet&lt;string&gt; Data = new HashSet&lt;string&gt;();
          public static IDictionary&lt;string, ConcurrentBag&lt;Test&gt;&gt; SereverConnections = new ConcurrentDictionary&lt;string, ConcurrentBag&lt;Test&gt;&gt;();
  
          public string SessionId { set; get; }
          public Test()
          {
              SessionId = Guid.NewGuid().ToString();
          }
          public void Add(string Val)
          {
              lock (Data) Data.Add(Val);
          }
  
          public void Remove(string Val)
          {
              lock (Data) Data.Remove(Val);
          }
  
  
          public void AddDictonary(Test Val)
          {
              ConcurrentBag&lt;Test&gt; connections;
              connections=SereverConnections["123"] = new ConcurrentBag&lt;Test&gt;();
  
              connections.Add(this);
          }
  
  
          public void RemoveDictonary(Test Val)
          {
              SereverConnections["123"].TryTake(out Val);
          }
  
          public override int GetHashCode()
          {
              return SessionId.GetHashCode();
          }
  
      }
  
  //calling
   class Program
      {
          static void Main(string[] args)
          {
              Test test = new Test();
  
              test.AddDictonary(test);
  
              test.RemoveDictonary(test);//remove test.
              test.RemoveDictonary(new Test());//new object
          }
      }
  </code></pre>