tags:

views:

227

answers:

12

I need to create a List<> of pairs of my classes. such as: List<Class1, Class2>

How would you do it? I can not use the "Pair" datatype because it's in system.web.UI.

Would you create a list of arrays? Create a struct of both classes and add them to a list?

Is there another way I don't know of and I'm just a noob?

Note: I don't want the list to auto-sort in any way.

+1  A: 

You could create your own Pair class ...

Hans Kesting
Or struct. A value type might be more appropriate than a reference type.
Jon Skeet
+7  A: 

I just use this:

var list = List<KeyValuePair<String, String>>;

or any other data type of course. It means you are accessing it like list(0).Key and list(0).Value but this doesn't bother me unless I am exposing the list externally.

Pondidum
Will it autosort my list? (I don't want to)
Faruz
No - a List is essentially a resizable array - every element will be a KeyValuePair. They will be maintianed in the order you add the elements.
thecoop
+1  A: 

It's designed to be used in Dictionaries, but you could use a KeyValuePair

Matthew Steeples
+1  A: 

Can you use a generic Dictiontary instead of a generic list?

Lee Hesselden
+1  A: 

You could simply use a Dictionary<>

Tim
Except a `Dictionary<>` doesn't maintain item order like a `List<>` does.
RickNZ
A `SortedDictionary<>` does. AFAIK anyways...
Svish
Svish. No. SortedDictionary sorts by key, not by insertion order
Esben Skov Pedersen
+1  A: 

As Hans Kesting points out, you could create your own Pair class, but often such a construct tends to cover up the fact that the relationship between the two types you are trying to pair actually represent a stronger concept in your API.

So instead of ending up with a generic Pair (or Tuple) your code may be more readable if you explicitly type the pair as a strong concept with good naming.

Mark Seemann
system.drawing Point and Size are good examples of this
jk
+3  A: 

I know some people get uncomfortable about this - see a discussion here - but there's nothing to stop you referencing System.Web in a non-web project, and then you can use System.Web.UI.Pair quite happily.

PhilPursglove
I think this is the best approach, why re-invent the wheel
naspinski
My teammates will hate me.. but I'll go with that (I'm extremely embarrassed though and really hate microsoft for making "Pair" available only on the web.
Faruz
Pair's not something I've come across before but it does look so generic that it should be in the System namespace, not System.Web.UI.
PhilPursglove
Poor solution really - this is overkill in the situation. Why introduce a dependency for such a simple class construct? "Don't re-invent the wheel" is fair enough, but Pair is such a simple construct it's not worth adding dependencies to System.Web for.
David_001
+8  A: 

Simply create your own pair class:

class Pair<TFirst, TSecond> {

  public TFirst First { get; set; }

  public TSecond Second { get; set; }

}

Initialize it like this:

Pair<String, String> pair = new Pair<String, String> {
  First = "first",
  Second = "second"
};

The next release of .NET (4.0) has tuples which is a more general solution to the problem.

Martin Liversage
This is a much better solution than using KeyValuePair as mentioned elsewhere, as "KeyValuePair" implies there is a Key / Value relationship, which there probably isn't (in the OP's question).
David_001
A: 

I would go with either KeyValuePair (as used by Dictionary) or create my own struct that acts as a typed container, emphasising the relation between the types.

By the time I've typed this several people have posted much the same - but I'll post this anyway for reassurance :-)

Phil Nash
A: 

One answer to everybody (It's the same answer and I don't want to add comments for everyone...):

I feel like using Key-Value combinations (or dictionary) is not natural because it's not representing a key and a value.

I'm trying to avoid creating my own class to represent the pair.

Maybe I'll import the System.Web.UI... feels crappy about it though

Faruz
+1  A: 

I agree with using a dictionary unles you can have mulitple values that are the same as dictionaries require unique Key values.

If you really needed pairs, you could make a class to do it and use it like this:

List<Pair<string, int>> pairs = new List<Pair<string, int>>();
pairs.Add(new Pair<string, int>("entry1", 1));
pairs.Add(new Pair<string, int>("entry2", 2));

Class

public class Pair<T, T2>
{
    T Value1;
    T2 Value2;
     public Pair(T value1, T2 value2)
     {
         Value1 = value1;
         Value2 = value2;
     }
}

I used generics because you didn't specify what type (this will work with any 2 Types). you could simplify it even more if you hard-coded the types.

naspinski
This way will work, but I agree with PhilPursglove - just reference System.Web.UI.Pair
naspinski
+1  A: 

Surely anonymous types are suited perfectly for this:

var Pair = new { Part1 = "A", Part2 = 6 };
var PairList = (new[] { Pair }).ToList();
PairList.Add(new { Part1  = "B", Part2  = 9 });

...

List[0].Part1 = "C";

Basically this is making a new "var object" containing a string and an int. Adding it to an array and invoking the ToList() extension method provided by System.Linq on the array to return an object which is effectively List<"typeof(Pair)"> object.

(EDIT)

Or neater still:

var PairList2 = (new[] { new { Part1 = "A", Part2 = 6 } }).ToList();

(ideas pinched from: http://kirillosenkov.blogspot.com/2008/01/how-to-create-generic-list-of-anonymous.html)

Rick
Don't think anonymous types are supported in framework 2, right?
Faruz
Let me rephrase: I'm sure they are not supported in framework 2... shame...
Faruz
I think you're right... I'm spoilt with 3.5
Rick