什么是 C++ std::pair 的 C# 模拟? [英] What is C# analog of C++ std::pair?
本文介绍了什么是 C++ std::pair 的 C# 模拟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我感兴趣:C# 中的 std::pair
在 C++ 中的模拟是什么?我找到了 System.Web.UI.Pair
类,但我更喜欢基于模板的东西.
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.
谢谢!
推荐答案
元组 自 .NET4.0 起可用 并支持泛型:
Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
<小时>
在以前的版本中,您可以使用 System.Collections.Generic.KeyValuePair
或类似以下的解决方案:
In previous versions 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; }
};
并像这样使用它:
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
这篇关于什么是 C++ std::pair 的 C# 模拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文