Lets say that I have a List of a custom "Point" class (I know there is one in System.Drawing, but lets say I need a custom one). Now this list can sometimes have the same points, so for example, say it is set up like this:
List<customPoint> myPoints = new List<customPoint>();
myPoints.Add(new customPoint(1,5));
myPoints.Add(new customPoint(1,5));
myPoints.Add(new customPoint(2,3));
myPoints.Add(new customPoint(4,9));
myPoints.Add(new customPoint(8,7));
myPoints.Add(new customPoint(2,3));
And later I need to do some calculation, but I do not need duplicates. What would be a more elegant way to make a new list of unique points than this:
List<customPoint> uniquePoints = new List<customPoint>();
for(int i; i < myPoints.Count; i++)
{
Boolean foundDuplicate = false;
int tempX = myPoints[i].X;
int tempY = myPoints[i].Y;
for(int j=0; j < uniquePoints.Count; j++)
{
if((tempX == uniquePoints[0].X) && (tempY == uniquePoints[0].Y))
{
foundDuplicate = true;
break;
}
}
if(!foundDuplicate)
{
uniquePoints.Add(myPoints[i]);
}
}
I know it is messy, but that is why I am asking if there is a more elegant way. I looked at the Linq "Distinct" command, but it does not appear to work, I guess there's something in their object instantiation that is still unique.