2008-10-03 54 views
243

我对C++ std :: pair的C#模拟器感兴趣吗?我找到了System.Web.UI.Pair类,但想要基于模板的东西。C++ std :: pair的C#模拟是什么?

谢谢!

+10

我有同样的请求,而以前,但我越想到它,你可能想要推出自己的配对类,具有明确的类类型和字段,而不是通用的“第一”和“第二”。它使你的代码更具可读性。配对类可以少至4行,所以通过重新使用泛型对并不会节省太多代码,并且代码更具可读性。 – 2012-04-30 20:57:57

回答

266

元组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 
+0

查看我的帖子关于添加Equals方法 – 2009-10-23 18:16:22

+0

Tuple <>现在是更好的解决方案。 – dkantowitz 2012-08-03 07:06:41

+5

由于无法在对象创建表达式(构造函数调用)中推断出属于泛型类的类型参数,因此BCL的作者制作了一个非泛型辅助类“Tuple”。因此你可以说`Tuple.Create(“Hello”,4)`这比`新Tuple (“Hello”,4)`更容易。 (顺便说一下,自2010年以来,.NET4.0已经在这里了。) – 2012-08-15 18:46:15

6

如果它是关于字典之类的,你要找的系统。 Collections.Generic.KeyValuePair < TKey,TValue >。

36

不幸的是,没有。在许多情况下,您可以使用System.Collections.Generic.KeyValuePair<K, V>

或者,您可以使用匿名类型来处理的元组,至少局部:

var x = new { First = "x", Second = 42 }; 

最后的选择是创建一个自己的类。

+2

为了清楚起见,匿名类型也是只读的[msdn](http://msdn.microsoft.com/zh-cn/library/bb397696.aspx)。 – bsegraves 2012-03-16 19:09:39

3

取决于你想完成什么,你可能想试试KeyValuePair

您无法更改条目的键值这一事实当然可以通过将整个条目替换为KeyValuePair的新实例来纠正。

2

我是一个快速谷歌,我发现有一个在.NET中对类除后问同样的问题刚才它在System.Web.UI程序^〜^(http://msdn.microsoft.com/en-us/library/system.web.ui.pair.aspx) 天知道他们为什么把它那里而不是集合框架

+0

我知道System.Web.UI.Pair。尽管通用类。 – 2008-10-15 04:00:20

+0

System.Web.UI.Pair是密封的。你不能从它派生(如果你想添加类型安全访问器)。 – 2010-02-11 17:52:41

3

我创建了一个元组的C#实现,它通常解决了两个和五个值之间的问题 - here's the blog post,其中包含到源的链接。

76

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); 
20

C#有tuples为4.0版本。

1

为了得到上述工作(我需要一对作为字典的关键)。我不得不添加:

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(); 
    } 

抑制编译器警告。

11

一些答案似乎只是错误的,

  1. 不能使用字典将如何存储对(a,b)和(A,C)。对概念不应该与关键相关查找了混淆和值
  2. 很多上面的代码似乎嫌疑

这是我对类

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)); 
    } 
} 
2

由于.NET 4.0中,您有System.Tuple<T1, T2>类:

// pair is implicitly typed local variable (method scope) 
var pair = System.Tuple.Create("Current century", 21); 
2

我通常延长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); 
1
从自定义类

除了或.Net 4.0元组,因为C#7.0有一个名为ValueTuple的新特性,它是一个可以在这种情况下使用的结构。而是写:

Tuple<string, int> t = new Tuple<string, int>("Hello", 4); 

并获得价值通过t.Item1t.Item2,你可以简单地做这样的:

(string message, int count) = ("Hello", 4); 

甚至:

(var message, var count) = ("Hello", 4); 
相关问题