我很感兴趣:C#在C ++中对std::pair的模拟是什么?我找到了System.Web.UI.Pair类,但我更喜欢基于模板的东西。

谢谢!

有帮助吗?

解决方案

自.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

其他提示

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

有关更多语法示例,请参阅 MSDN

2012年6月更新 Tuples 自4.0版以来一直是.NET的一部分。

以下是前一篇文章,描述了在.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 };

最后一种方法是创建一个自己的类。

自版本4.0开始,C#已元组

有些答案似乎错了,

  1. 你不能使用字典如何存储对(a,b)和(a,c)。不应将对概念与关键和值的关联查找混淆
  2. 以上许多代码似乎都很可疑
  3. 这是我的配对类

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

如果它是关于字典之类的,那么你正在寻找System.Collections.Generic.KeyValuePair <!> lt; TKey,TValue <!> gt;。

根据您想要完成的任务,您可能需要尝试 KeyValuePair

您无法更改条目键的事实当然可以通过简单地用KeyValuePair的新实例替换整个条目来纠正。

我创建了一个Cuples的C#实现,它通常解决了两到五个值之间的问题 - 这是博客文章,其中包含指向源代码的链接。

我刚刚在快速谷歌之后问了同样的问题我发现除了它在System.Web.UI中有一对.NET之外^〜^( http://msdn.microsoft.com/en-us/library/system.web.ui.pair.aspx ) 善良知道他们为什么把它放在那里而不是收藏框架

从.NET 4.0开始,你有System.Tuple<T1, T2> class:

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

为了使上述工作(我需要一对作为字典的键)。我不得不补充道:

    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结构。

除了自定义类或.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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top