回答
元组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
查看我的帖子关于添加Equals方法 – 2009-10-23 18:16:22
Tuple <>现在是更好的解决方案。 – dkantowitz 2012-08-03 07:06:41
由于无法在对象创建表达式(构造函数调用)中推断出属于泛型类的类型参数,因此BCL的作者制作了一个非泛型辅助类“Tuple”。因此你可以说`Tuple.Create(“Hello”,4)`这比`新Tuple
如果它是关于字典之类的,你要找的系统。 Collections.Generic.KeyValuePair < TKey,TValue >。
不幸的是,没有。在许多情况下,您可以使用System.Collections.Generic.KeyValuePair<K, V>
。
或者,您可以使用匿名类型来处理的元组,至少局部:
var x = new { First = "x", Second = 42 };
最后的选择是创建一个自己的类。
为了清楚起见,匿名类型也是只读的[msdn](http://msdn.microsoft.com/zh-cn/library/bb397696.aspx)。 – bsegraves 2012-03-16 19:09:39
取决于你想完成什么,你可能想试试KeyValuePair。
您无法更改条目的键值这一事实当然可以通过将整个条目替换为KeyValuePair的新实例来纠正。
我是一个快速谷歌,我发现有一个在.NET中对类除后问同样的问题刚才它在System.Web.UI程序^〜^(http://msdn.microsoft.com/en-us/library/system.web.ui.pair.aspx) 天知道他们为什么把它那里而不是集合框架
我知道System.Web.UI.Pair。尽管通用类。 – 2008-10-15 04:00:20
System.Web.UI.Pair是密封的。你不能从它派生(如果你想添加类型安全访问器)。 – 2010-02-11 17:52:41
我创建了一个元组的C#实现,它通常解决了两个和五个值之间的问题 - here's the blog post,其中包含到源的链接。
System.Web.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月:Tuples
自4.0版以来一直是.NET的一部分。
这里是仿制药an earlier article describing inclusion in.NET4.0和支持:
Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
C#有tuples为4.0版本。
为了得到上述工作(我需要一对作为字典的关键)。我不得不添加:
public override Boolean Equals(Object o)
{
Pair<T, U> that = o as Pair<T, U>;
if (that == null)
return false;
else
return this.First.Equals(that.First) && this.Second.Equals(that.Second);
}
,一旦我这样做,我还添加了
public override Int32 GetHashCode()
{
return First.GetHashCode()^Second.GetHashCode();
}
抑制编译器警告。
PowerCollections库(以前可用于Wintellect,但现在托管在Codeplex @http://powercollections.codeplex.com上)具有通用Pair结构。
一些答案似乎只是错误的,
- 不能使用字典将如何存储对(a,b)和(A,C)。对概念不应该与关键相关查找了混淆和值
- 很多上面的代码似乎嫌疑
这是我对类
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));
}
}
由于.NET 4.0中,您有System.Tuple<T1, T2>
类:
// pair is implicitly typed local variable (method scope)
var pair = System.Tuple.Create("Current century", 21);
我通常延长Tuple
类到我自己的通用包装如下:
public class Statistic<T> : Tuple<string, T>
{
public Statistic(string name, T value) : base(name, value) { }
public string Name { get { return this.Item1; } }
public T Value { get { return this.Item2; } }
}
,并使用它,像这样:
public class StatSummary{
public Statistic<double> NetProfit { get; set; }
public Statistic<int> NumberOfTrades { get; set; }
public StatSummary(double totalNetProfit, int numberOfTrades)
{
this.TotalNetProfit = new Statistic<double>("Total Net Profit", totalNetProfit);
this.NumberOfTrades = new Statistic<int>("Number of Trades", numberOfTrades);
}
}
StatSummary summary = new StatSummary(750.50, 30);
Console.WriteLine("Name: " + summary.NetProfit.Name + " Value: " + summary.NetProfit.Value);
Console.WriteLine("Name: " + summary.NumberOfTrades.Value + " Value: " + summary.NumberOfTrades.Value);
除了或.Net 4.0元组,因为C#7.0有一个名为ValueTuple的新特性,它是一个可以在这种情况下使用的结构。而是写:
Tuple<string, int> t = new Tuple<string, int>("Hello", 4);
并获得价值通过t.Item1
和t.Item2
,你可以简单地做这样的:
(string message, int count) = ("Hello", 4);
甚至:
(var message, var count) = ("Hello", 4);
- 1. php有C++的std :: pair吗?
- 2. C fread()的C#模拟是什么?
- 3. C snprintf()的C#模拟是什么?
- 4. 在C++中继承std :: pair和std :: map
- 5. C++模拟ContainsKey(TKey)来自C# - 最好的实践是什么?
- 6. C++什么是std :: shared_future和std :: promise
- 7. C中std :: pair的等效形式
- 8. C++ const正确性与std :: pair
- 9. C++ STL map,std :: pair作为密钥
- 10. 模板类中声明std :: pair的C++错误
- 11. std :: pair作为模板<class>的参数? C++
- 12. 为什么std :: pair没有迭代器?
- 13. C++:什么是std :: function constness的语义?
- 14. 这是什么C++错误的std :: length_error
- 15. 为什么std :: pair <int, int>可以从const std :: pair <int, float>&?
- 16. 什么的.NET Buffer.BlockCopy的纯C模拟?
- 17. 地图与std :: pair地图的优点是什么?
- 18. 什么是'std :;'在C++中做?
- 19. 什么是C#等价于std :: vector :: iterators?
- 20. C++无法在std :: pair中将lambda转换为std :: packaged_task
- 21. cppman模拟C?
- 22. 什么是std :: _ Bind <std :: _ Mem_fn <void(ClassName :: *)()>(ClassName *)>是什么意思? (C++)
- 23. Key Value Pair C
- 24. ** std :: setprecision()**没有** std :: fixed **在C++中的作用是什么?
- 25. 什么是python std :: lower_bound和std :: upper_bound C++算法的等价物?
- 26. 什么是C#的AutoResetEvent/ManualResetEvent的便携式模拟?
- 27. 什么是在C++
- 28. C#中lua`local variable = var1或var2`的模拟是什么?
- 29. C#中术语“模拟”的含义是什么?
- 30. 在C#中模拟雷达的最佳方法是什么?
我有同样的请求,而以前,但我越想到它,你可能想要推出自己的配对类,具有明确的类类型和字段,而不是通用的“第一”和“第二”。它使你的代码更具可读性。配对类可以少至4行,所以通过重新使用泛型对并不会节省太多代码,并且代码更具可读性。 –
2012-04-30 20:57:57