我感兴趣的是C++中的C std::pair
的模拟是什么?我找到了System.Web.UI.Pair
类,但我更喜欢基于模板的.
非常感谢.
我感兴趣的是C++中的C std::pair
的模拟是什么?我找到了System.Web.UI.Pair
类,但我更喜欢基于模板的.
非常感谢.
元组are available since .NET4.0和支持泛型:
Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
在以前的版本中,您可以使用System.Collections.Generic.KeyValuePair<K, V>
或如下所示的解决方案:
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; }
};
这样使用:
Pair<String, int> pair = new Pair<String, int>("test", 2);
Console.WriteLine(pair.First);
Console.WriteLine(pair.Second);
这将产生:
test
2
甚至这种链式配对:
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);
这将产生:
test
12
true