Ale*_*yev 277 .net c# base-class-library data-structures std-pair
我很感兴趣:C#std::pair
在C++中的模拟是什么?我找到了System.Web.UI.Pair
课程,但我更喜欢基于模板的课程.
谢谢!
Jor*_*ira 313
从.NET4.0开始提供元组并支持泛型:
Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
Run Code Online (Sandbox Code Playgroud)
在以前的版本中,您可以使用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; }
};
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
Pair<String, int> pair = new Pair<String, int>("test", 2);
Console.WriteLine(pair.First);
Console.WriteLine(pair.Second);
Run Code Online (Sandbox Code Playgroud)
这输出:
test
2
Run Code Online (Sandbox Code Playgroud)
甚至这个链式对:
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);
Run Code Online (Sandbox Code Playgroud)
那输出:
test
12
true
Run Code Online (Sandbox Code Playgroud)
Jay*_*ker 88
System.Web.UI
包含Pair
该类,因为它在ASP.NET 1.1中被大量使用作为内部ViewState结构.
2017年8月更新: C#7.0/.NET Framework 4.7提供了使用System.ValueTuple
struct 声明具有命名项的元组的语法.
//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);
Run Code Online (Sandbox Code Playgroud)
有关更多语法示例,请参阅MSDN.
2012年6月更新: Tuples
自4.0版以来一直是.NET的一部分.
这是一篇早期文章,描述了.NET4.0中的包含和对泛型的支持:
Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
Run Code Online (Sandbox Code Playgroud)
Kon*_*lph 38
不幸的是,没有.你可以System.Collections.Generic.KeyValuePair<K, V>
在很多情况下使用它.
或者,您可以使用匿名类型来处理元组,至少在本地:
var x = new { First = "x", Second = 42 };
Run Code Online (Sandbox Code Playgroud)
最后一种方法是创建一个自己的类.
小智 11
一些答案似乎错了,
这是我的配对课程
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是一些测试代码:
[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));
}
}
Run Code Online (Sandbox Code Playgroud)
除了自定义类或 .Net 4.0 元组之外,从 C# 7.0 开始,还有一个名为 ValueTuple 的新功能,它是一个可以在这种情况下使用的结构。而不是写:
Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
Run Code Online (Sandbox Code Playgroud)
并通过t.Item1
和访问值t.Item2
,您可以简单地这样做:
(string message, int count) = ("Hello", 4);
Run Code Online (Sandbox Code Playgroud)
甚至:
(var message, var count) = ("Hello", 4);
Run Code Online (Sandbox Code Playgroud)