什么是C#模拟C ++的std ::对? [英] What is C# analog of C++ std::pair?
本文介绍了什么是C#模拟C ++的std ::对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我感兴趣的是C#模拟C ++的std ::对?我发现System.Web.UI.Pair类,但希望基于模板的东西。
I am interested what is C# analog of C++ std::pair? I have found System.Web.UI.Pair class, but wanted something template based.
感谢您!
推荐答案
元组将成为其中的一部分。 NET4.0 并支持泛型:
Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
您可以使用 System.Collections.Generic.KeyValuePair&LT; K,V&GT;
或像下面这样的解决方案:
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
甚至这个链接对:
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);
这是输出:
test
12
true
这篇关于什么是C#模拟C ++的std ::对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文