I'm interested: What is C#'s analog of std::pair
in C++? I found System.Web.UI.Pair
class, but I'd prefer something template-based.
Thank you!
I'm interested: What is C#'s analog of std::pair
in C++? I found System.Web.UI.Pair
class, but I'd prefer something template-based.
Thank you!
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.
Tuples are available since .NET4.0 and support generics:
In previous versions you can use
System.Collections.Generic.KeyValuePair<K, V>
or a solution like the following:And use it like this:
This outputs:
Or even this chained pairs:
That outputs:
If it's about dictionaries and the like, you're looking for System.Collections.Generic.KeyValuePair<TKey, TValue>.
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
Since .NET 4.0 you have
System.Tuple<T1, T2>
class:Apart from custom class or .Net 4.0 Tuples, since C# 7.0 there is a new feature called ValueTuple, which is a struct that can be used in this case. Instead of writing:
and access values through
t.Item1
andt.Item2
, you can simply do it like that:or even: