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!
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!
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
If it's about dictionaries and the like, you're looking for System.Collections.Generic.KeyValuePair<TKey, TValue>.
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.
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.
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 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.
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);
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.
The PowerCollections library (formerly available from Wintellect but now hosted on Codeplex @ http://powercollections.codeplex.com) has a generic Pair structure.