views:

17758

answers:

11

I am interested what is C# analog of C++ std::pair? I have found System.Web.UI.Pair class, but wanted something template based.

Thank you!

+52  A: 

You can use System.Collections.Generic.KeyValuePair<K, V> or a solution like the following:

public class Pair<T, U> {
    public Pair() {
    }

    public Pair(T first, U second) {
        this.First = first;
        this.Second = second;
    }

    public T First { get; set; }
    public U Second { get; set; }
};

And use it like this:

Pair<String, int> pair = new Pair<String, int>("test", 2);
Console.WriteLine(pair.First);
Console.WriteLine(pair.Second);

This outputs:

test
2

Or even this chained pairs:

Pair<Pair<String, int>, bool> pair = new Pair<Pair<String, int>, bool>();
pair.First = new Pair<String, int>();
pair.First.First = "test";
pair.First.Second = 12;
pair.Second = true;

Console.WriteLine(pair.First.First);
Console.WriteLine(pair.First.Second);
Console.WriteLine(pair.Second);

That outputs:

test
12
true
smink
Tweak casing in the second constructor.
AnthonyWJones
you are right :) my bad.
smink
See my post about adding a Equals method
Andrew Stein
This is perfect for what I need.
Donblas
+5  A: 

If it's about dictionaries and the like, you're looking for System.Collections.Generic.KeyValuePair<TKey, TValue>.

OregonGhost
+15  A: 

Unfortunately, there is none. You can use the System.Collections.Generic.KeyValuePair<K, V> in many situations.

Alternatively, you can use anonymous types to handle tuples, at least locally:

var x = new { First = "x", Second = 42 };

The last alternative is to create an own class.

Konrad Rudolph
+2  A: 

Depending on what you want to accomplish, you might want to try out KeyValuePair.

The fact that you cannot change the key of an entry can of course be rectified by simply replacing the entire entry by a new instance of KeyValuePair.

Grimtron
+2  A: 

I was asking the same question just now after a quick google I found that There is a pair class in .NET except its in the System.Web.UI ^ ~ ^ (http://msdn.microsoft.com/en-us/library/system.web.ui.pair.aspx) goodness knows why they put it there instead of the collections framework

I know about System.Web.UI.Pair. Wanted generic class though.
Alexander Prokofyev
System.Web.UI.Pair is sealed. You cannot derive from it (in case you want to adde type safe accessors).
Martin Vobr
+3  A: 

I created a C# implementation of Tuples, which solves the problem generically for between two and five values - here's the blog post, which contains a link to the source.

Erik Forbes
+11  A: 

I believe System.Web.UI contained the Pair class because it was used heavily in ASP.NET 1.1 as an internal ViewState structure.

Tuples will be part of .net 4.0 (http://msdn.microsoft.com/en-us/magazine/dd942829.aspx) and supports generics.

Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
JWalker
That link is a great read.
280Z28
+4  A: 

The C# 4 will have tuples.

kek444
+2  A: 

On order to get the above to work (I needed a pair as the key of a dictionary). I had to add:

    public override Boolean Equals(Object o)
    {
        Pair<T, U> that = o as Pair<T, U>;
        if (that == null)
            return false;
        else
            return this.First.Equals(that.First) && this.Second.Equals(that.Second);
    }

and once I did that I also added

    public override Int32 GetHashCode()
    {
        return First.GetHashCode() ^ Second.GetHashCode();
    }

to suppress a compiler warning.

Andrew Stein
You should find a better hash-code algorithm than that, try using 37+23*(h1+23*(h2+23*(h3+...))) This will make (A,B) distinct from (B,A), ie. reordering will have an effect on the code.
Lasse V. Karlsen
Comment is a accepted.. In my case I was just trying to suppress the compiler waning, and anyway T is a String and U an Int32...
Andrew Stein
A: 

The PowerCollections library (formerly available from Wintellect but now hosted on Codeplex @ http://powercollections.codeplex.com) has a generic Pair structure.

James Webster
+1  A: 
Antony