Maxim V. Pavlov
Maxim V. Pavlov

Reputation: 10509

Two-dimensional array with .Add in C#

I need to store a collection of Point pairs, but don't want to use plain C# arrays to be bound to use of iterators. Which is the easiest way (except using KeyValuePair<Point,Point>) to have such an two dimensional collection at hand with being able to .Add(Point, Point) to it?

Upvotes: 3

Views: 1650

Answers (4)

csharpnumpty
csharpnumpty

Reputation: 3723

IEnumerable<Tuple<Point, Point>> 

http://msdn.microsoft.com/en-us/library/dd268536.aspx

Upvotes: 1

Vladislav Zorov
Vladislav Zorov

Reputation: 3056

Point,Point is not a key-value pair, it's a Tuple<Point,Point>. You can create a List<Tuple<Point,Point>> or whatever fits your needs.

However, you should keep something in mind - a Tuple is an immutable structure - you can't change it's elements to point to other Point objects (however, if the Point object itself is mutable, you could mutate the Point). If you need to be able to change elements from this list to point to other points, create your own class to hold these.

Upvotes: 4

tomfanning
tomfanning

Reputation: 9670

Subclass List<Tuple<Point,Point>> and create an overload of .Add with two Point parameters.

public class MyCollection : List<Tuple<Point, Point>>
{
    public void Add(Point p1, Point p2)
    {
        this.Add(Tuple.Create(p1, p2));
    }
}

Upvotes: 4

Lev
Lev

Reputation: 3924

will List<Tuple<int,int>> you help?

Upvotes: 1

Related Questions