Duplicate values in generic list c#
<p>I'm adding values to a c# generic list while trying to prevent duplicates, but without success.  Anyone know of a reason why this code below wouldn't work?</p>

<p>I have a simple class here:</p>

<pre><code>public class DrivePairs
{
    public int Start { get; set; }
    public int End { get; set; }
}
</code></pre>

<p>And here is my method which tries to return a generic list of the above class:</p>

<pre><code>ArrayList found = DriveRepository.GetDriveArray(9, 138);
List&lt;DrivePairs&gt; drivePairs = new List&lt;DrivePairs&gt;();
foreach (List&lt;int&gt; item in found)
{
    int count = item.Count;
    if (count &gt; 1)
    {
        for (int i = 0; i &lt; (count - 1); i++)
        {
            DrivePairs drivePair = new DrivePairs();
            drivePair.Start = item[i];
            drivePair.End = item[i + 1];

            if (!drivePairs.Contains(drivePair))
                drivePairs.Add(drivePair);
        }
    }
}
drivePairs = drivePairs.Distinct().ToList();
</code></pre>

<p>As you can maybe see, I have an <code>ArrayList</code>, and each row contains a <code>List&lt;int&gt;</code>.  What I'm doing is going through each and adding to a list which contains only pairs.  E.g. if my <code>List&lt;int&gt;</code> contains <code>[1,3,6,9]</code> I want to add three entries to my pairs list: </p>

<pre><code>[1,3]
[3,6]
[6,9]
</code></pre>

<p>It all works fine apart from not recognising duplicates.  I thought this line would be enough:</p>

<pre><code>if (!drivePairs.Contains(drivePair))
    drivePairs.Add(drivePair);
</code></pre>

<p>but it continues to add them all.  Even when I add a <code>Distinct()</code> at the end, it still doesn't remove them.  I've also tried adding them to a <code>HashSet</code>, but it still includes all the duplicates.</p>

<p>Anyone know of a reason why the duplicates might not be getting picked up?</p>
