title: Preventing Duplicate List<T> Entries
body: <p>I expect I'll be able to make a work around but I can't for the life of me understand why this code is not functioning correctly and allowing duplicate entries to be added to the List.</p>
  
  <p>The <code>if</code> statement condition is never met, even when I drag identical files in from the same location. I don't understand why the "Contains" method isn't matching them up.</p>
  
  <p><img src="https://i.stack.imgur.com/5hiIr.png" alt=""></p>
  
  <pre><code>public class Form1:Form {
      private List&lt;FileInfo&gt; dragDropFiles = new List&lt;FileInfo&gt;();
  
      private void Form1_DragDrop(object sender, DragEventArgs e) {
          try {
              if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
                  string[] files =
                      (string[])e.Data.GetData(DataFormats.FileDrop);
  
                  OutputDragDrop(files);
              }
          }
          catch { }
      }
  
      private void Form1_DragEnter(object sender, DragEventArgs e) {
          if (e.Data.GetDataPresent(DataFormats.FileDrop))
              e.Effect = DragDropEffects.Copy;
          else
              e.Effect = DragDropEffects.None;
      }
  
      private void OutputDragDrop(string[] files) {
          try {
              foreach (string file in files) {
                  FileInfo fileInfo = new FileInfo(file);
  
                  if (dragDropFiles.Contains(fileInfo)) {
                      dragDropFiles.Remove(fileInfo);
                  }
                  dragDropFiles.Add(fileInfo);
              }
              PopulateContextMenu();
          }
          catch { }
      }
  }
  </code></pre>
  
  <p>I thought I had found another method in which to achieve this using "Distinct"</p>
  
  <p>However, it appears <code>checkedDragDropFiles</code> &amp; <code>dragDropFiles</code> have the same amount of entries, including duplicates, except when <code>dragDropFiles</code> is displayed in a <code>ListBox</code> it doesn't show them. Why does it do this?</p>
  
  <p>I need to prevent any duplicated list entries, as I would be programmatically creating a menu based off of the list data.</p>
  
  <pre><code>private void OutputDragDrop(string[] files)
  {
      try
      {
          foreach (string file in files)
          {
              FileInfo fileInfo = new FileInfo(file);
  
              //if (dragDropFiles.Contains(fileInfo))
              //{
              //    dragDropFiles.Remove(fileInfo);
              //}
              dragDropFiles.Add(fileInfo);
          }
  
          List&lt;FileInfo&gt; checkedDragDropFiles = dragDropFiles.Distinct().ToList();
  
          debugList.DataSource = checkedDragDropFiles;
          debugList2.DataSource = dragDropFiles;
          //PopulateContextMenu();
      }
      catch { }
  }
  </code></pre>