什么是C ++ std :: pair的C#类似物?

时间:2020-03-06 15:03:57  来源:igfitidea点击:

我感兴趣的是C ++ std :: pair的Canalog?我找到了System.Web.UI.Pair类,但是想要一些基于模板的东西。

谢谢!

解决方案

从.NET4.0开始,元组可用,并支持泛型:

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

在以前的版本中,可以使用System.Collections.Generic.KeyValuePair &lt;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.Collections.Generic.KeyValuePair <TKey,TValue>。

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

另外,我们可以使用匿名类型至少在本地处理元组:

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

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

根据我们要完成的工作,我们可能想尝试KeyValuePair。

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

在快速谷歌搜索之后,我刚才问了同样的问题。我发现.NET中有一个配对类,但System.Web.UI中却存在^〜^(http://msdn.microsoft.com/zh-cn/库/system.web.ui.pair.aspx)
天哪知道为什么他们把它放在那里而不是收藏架

我创建了一个Cimplementation of Tuples(元组的实现),该问题通常可以解决两个到五个值之间的问题,这是博客文章,其中包含到源的链接。

System.Web.UI包含对类,因为在ASP.NET 1.1中大量使用它作为内部ViewState结构。

2017年8月更新:C7.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版起," Tuples"已成为.NET的一部分。

这是一篇较早的文章,介绍了.NET4.0中的包含和对泛型的支持:

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

从版本4.0开始的Chas元组。

为了使上述内容起作用(我需要一对作为字典的键)。我必须添加:

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