Hey kind people! How do I remove duplicate Rectangles from my List<Rectangle> in C#?

Thank you

Recommended Answers

All 10 Replies

This should do the trick:

private void button1_Click(object sender, EventArgs e)
    {
      List<Rectangle> lst = new List<Rectangle>();
      Rectangle rect;
      rect = new Rectangle(1, 2, 3, 4);
      lst.Add(rect);
      rect = new Rectangle(1, 2, 3, 4);
      lst.Add(rect);
      rect = new Rectangle(4, 3, 2, 1);
      lst.Add(rect);
      lst = lst.Distinct().ToList();
    }

Please mark this thread as solved if it answered your question and good luck!

Hi sknake,

I've tried that code and it didn't work in my solution, so I created a quick console app and sure enough it worked!

It seems my problem is that i'm not using the System.Drawing.Rectangle in my list of rectangles, instead am using the Microsoft.Xna.Framework.Rectangle to create my list.

How can I perform the same operation you have showed me, but on a List<Microsoft.Xna.Framework.Rectangle> ?

Thank you for your time and help

I have no idea. I don't know how to reference that assembly. Can you upload your project?

It should work for List<Microsoft.Xna.Framework.Rectangle> , which implements IEquatable<Microsoft.Xna.Framework.Rectangle> . Maybe you're expecting the code to modify the original list object. It doesn't do this; it instead creates a copy that lacks duplicates.

I have no idea. I don't know how to reference that assembly. Can you upload your project?

My project is huge so unfortunately uploading is not an option. :(

Use your own comparer is my only guess. I don't know enough about the XNA framework.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmRectangle : Form
  {
    public frmRectangle()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      List<Rectangle> lst = new List<Rectangle>();
      Rectangle rect;
      rect = new Rectangle(1, 2, 3, 4);
      lst.Add(rect);
      rect = new Rectangle(1, 2, 3, 4);
      lst.Add(rect);
      rect = new Rectangle(4, 3, 2, 1);
      lst.Add(rect);
      lst = lst.Distinct(new RectangleComparer()).ToList();
    }
  }
  public class RectangleComparer : IEqualityComparer<Rectangle>
  {
    public RectangleComparer()
    {
    }
    #region IEqualityComparer<Rectangle> Members
    public bool Equals(Rectangle x, Rectangle y)
    {
      return ((x.Left == y.Left) && (x.Height == y.Height) && (x.Y == y.Y) && (x.X == y.X));
    }
    public int GetHashCode(Rectangle obj)
    {
      return obj.GetHashCode();
    }
    #endregion
  }
}

It should work for List<Microsoft.Xna.Framework.Rectangle> , which implements IEquatable<Microsoft.Xna.Framework.Rectangle> . Maybe you're expecting the code to modify the original list object. It doesn't do this; it instead creates a copy that lacks duplicates.

Hey Rashakil,
Firstly, A little background about the app... I created WPF app, added an XNA class library, modified it to work inside WPF, created a class in WPF proj that inherits from Xna.Presentation.Game. Then I added this code to the class:

List<System.Drawing.Rectangle> list = new List<System.Drawing.Rectangle>();
            list.Add(new System.Drawing.Rectangle(1, 2, 3, 4));
            list.Add(new System.Drawing.Rectangle(1, 2, 3, 4));
            list.Add(new System.Drawing.Rectangle(5, 2, 3, 4));
            list.Add(new System.Drawing.Rectangle(7, 2, 3, 4));
            list.Add(new System.Drawing.Rectangle(5, 2, 3, 4));
            list.Add(new System.Drawing.Rectangle(1, 2, 3, 4));

            List<System.Drawing.Rectangle> unique = new List<System.Drawing.Rectangle>(list.Distinct<System.Drawing.Rectangle>());

Secondly, could you elaborate a little more on exactly what you mean?
The above code creates a copy of the list right?

I don't understand where its going wrong... VS simply tells me:

'System.Collections.Generic.List<System.Drawing.Rectangle>' does not contain a definition for 'Distinct' and no extension method 'Distinct' accepting a first argument of type 'System.Collections.Generic.List<System.Drawing.Rectangle>' could be found.

Thanks once again for your help

.Distinct() is an extension method defined in LINQ. Do you have a reference for it?

You need using System.Linq; at the top of your file.

Many thanks to you guys for helping e out with this! Looks like I had omitted the using statement:$

Onwards and upwards the project goes.. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.