Reputation: 9959
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.
Upvotes: 2
Views: 5568
Reputation: 3821
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)
Upvotes: 1
Reputation: 34689
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.
Upvotes: 1
Reputation: 9959
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
Upvotes: 0
Reputation: 22493
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 :-)
Upvotes: 0
Reputation: 106796
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.
Upvotes: 8
Reputation: 12589
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.
Upvotes: 3
Reputation: 233125
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.
Upvotes: 2
Reputation: 8058
It's designed to be used in Dictionaries, but you could use a KeyValuePair
Upvotes: 1
Reputation: 11617
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.
Upvotes: 9