title: C# Elegant way to handle checking for an item in a collection
body: <p>I've posted a code sample below. Firstly let me explain</p>
  
  <p>termStore.Groups in the code below is a collection of Group Objects (The exact class is irrelevant). </p>
  
  <p>Checking for null : if (termStore.Groups[groupName] == null) seems like a logical (clean) approach, but if the Groups collection is empty then an exception is produced. </p>
  
  <p>using the termStore.Groups.Contains is not an option either because this expects a strong type i.e: .Contains(Group)... not .Contains(GroupName as string)</p>
  
  <p>Can someone recommend a clean / generic way I can check for if an item exists in collection . </p>
  
  <p>Thank you....</p>
  
  <pre><code>TermStore termStore = session.TermStores.Where(ts =&gt; ts.Name == termStoreName).FirstOrDefault();
                  if (termStore.Groups[groupName] == null)
                  {
                      termStore.CreateGroup(groupName);
                      termStore.CommitAll();
                  }
  </code></pre>
  
  <p>Update: The exact class Sharepoint Taxonomy Classes. <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.group.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.group.aspx</a></p>
  
  <p>Update 2, the exact collection : <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.groupcollection.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.groupcollection.aspx</a></p>