我感兴趣的是:c++中类似std::pair的c#是什么?我找到System.Web.UI.Pair类,但我更喜欢基于模板的东西。
谢谢你!
我感兴趣的是:c++中类似std::pair的c#是什么?我找到System.Web.UI.Pair类,但我更喜欢基于模板的东西。
谢谢你!
当前回答
ui包含Pair类,因为它在ASP中被大量使用。NET 1.1作为内部ViewState结构。
2017年8月更新:c# 7.0 / . net Framework 4.7提供了一种语法,可以使用System声明具有命名项的元组。ValueTuple结构。
//explicit Item typing
(string Message, int SomeNumber) t = ("Hello", 4);
//or using implicit typing
var t = (Message:"Hello", SomeNumber:4);
Console.WriteLine("{0} {1}", t.Message, t.SomeNumber);
更多语法示例请参见MSDN。
2012年6月更新:元组从4.0版开始就成为。net的一部分。
以下是之前的一篇文章,描述了包含in.NET4.0和对泛型的支持:
Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
其他回答
ui包含Pair类,因为它在ASP中被大量使用。NET 1.1作为内部ViewState结构。
2017年8月更新:c# 7.0 / . net Framework 4.7提供了一种语法,可以使用System声明具有命名项的元组。ValueTuple结构。
//explicit Item typing
(string Message, int SomeNumber) t = ("Hello", 4);
//or using implicit typing
var t = (Message:"Hello", SomeNumber:4);
Console.WriteLine("{0} {1}", t.Message, t.SomeNumber);
更多语法示例请参见MSDN。
2012年6月更新:元组从4.0版开始就成为。net的一部分。
以下是之前的一篇文章,描述了包含in.NET4.0和对泛型的支持:
Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
不幸的是,没有。你可以使用System.Collections.Generic。KeyValuePair<K, V>在许多情况下。
或者,你可以使用匿名类型来处理元组,至少在本地:
var x = new { First = "x", Second = 42 };
最后一种选择是创建自己的类。
如果是关于字典之类的,你要找System.Collections.Generic。KeyValuePair < TKey, TValue >。
有些答案似乎是错的,
你不能使用字典来存储(a,b)和(a,c)对。对的概念不应与键和值的关联查找相混淆 上面的许多代码似乎都是可疑的
这是我的pair类
public class Pair<X, Y>
{
private X _x;
private Y _y;
public Pair(X first, Y second)
{
_x = first;
_y = second;
}
public X first { get { return _x; } }
public Y second { get { return _y; } }
public override bool Equals(object obj)
{
if (obj == null)
return false;
if (obj == this)
return true;
Pair<X, Y> other = obj as Pair<X, Y>;
if (other == null)
return false;
return
(((first == null) && (other.first == null))
|| ((first != null) && first.Equals(other.first)))
&&
(((second == null) && (other.second == null))
|| ((second != null) && second.Equals(other.second)));
}
public override int GetHashCode()
{
int hashcode = 0;
if (first != null)
hashcode += first.GetHashCode();
if (second != null)
hashcode += second.GetHashCode();
return hashcode;
}
}
下面是一些测试代码:
[TestClass]
public class PairTest
{
[TestMethod]
public void pairTest()
{
string s = "abc";
Pair<int, string> foo = new Pair<int, string>(10, s);
Pair<int, string> bar = new Pair<int, string>(10, s);
Pair<int, string> qux = new Pair<int, string>(20, s);
Pair<int, int> aaa = new Pair<int, int>(10, 20);
Assert.IsTrue(10 == foo.first);
Assert.AreEqual(s, foo.second);
Assert.AreEqual(foo, bar);
Assert.IsTrue(foo.GetHashCode() == bar.GetHashCode());
Assert.IsFalse(foo.Equals(qux));
Assert.IsFalse(foo.Equals(null));
Assert.IsFalse(foo.Equals(aaa));
Pair<string, string> s1 = new Pair<string, string>("a", "b");
Pair<string, string> s2 = new Pair<string, string>(null, "b");
Pair<string, string> s3 = new Pair<string, string>("a", null);
Pair<string, string> s4 = new Pair<string, string>(null, null);
Assert.IsFalse(s1.Equals(s2));
Assert.IsFalse(s1.Equals(s3));
Assert.IsFalse(s1.Equals(s4));
Assert.IsFalse(s2.Equals(s1));
Assert.IsFalse(s3.Equals(s1));
Assert.IsFalse(s2.Equals(s3));
Assert.IsFalse(s4.Equals(s1));
Assert.IsFalse(s1.Equals(s4));
}
}
我创建了一个元组的c#实现,它一般解决2到5个值之间的问题-这里是博客文章,其中包含一个到源代码的链接。