ChatGPT解决这个技术问题 Extra ChatGPT

什么是 C++ std::pair 的 C# 模拟?

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

谢谢!

前段时间我有同样的要求,但我想得越多,你可能只想推出自己的配对类,使用明确的类类型和字段,而不是通用的“First”和“Second”。它使您的代码更具可读性。一个配对类可以少至 4 行,因此通过重用通用 Pair 类不会节省太多,而且您的代码将更具可读性。

P
Pavel

元组 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 方法的帖子
Tuple<> 现在是一个更好的解决方案。
由于不能在对象创建表达式(构造函数调用)中推断属于泛型类的类型参数,因此 BCL 的作者制作了一个名为 Tuple 的非泛型辅助类。因此,您可以说 Tuple.Create("Hello", 4),它比 new Tuple<string, int>("Hello", 4) 容易一些。 (顺便说一句,.NET4.0 自 2010 年以来就已经出现了。)
请注意,Tuple<> 使用值语义实现了可靠的 EqualsGetHashCode,这非常棒。在实现自己的元组时请记住。
这显然是因为 Equals 和 GetHashCode 而被破坏的
J
Jay Walker

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

请注意,元组是只读的。也就是说,您不能这样做:tuple.Item1 = 4;
元组正是我想要的。谢谢。
K
Konrad Rudolph

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

或者,您可以使用匿名类型来处理元组,至少在本地:

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

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


需要说明的是,匿名类型也是只读的 - msdn
K
Kenan E. K.

C# 自 4.0 版起具有 tuples


J
Jay Walker

有些答案似乎是错误的,

您不能使用字典如何存储对 (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));
    }
}

如果您不实施 IEquatable,您将获得拳击。要正确完成课程,还有更多工作要做。
O
OregonGhost

如果是关于字典之类的,您正在寻找 System.Collections.Generic.KeyValuePair


L
LW001

除了自定义类或 .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);

E
Erik Forbes

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


G
Grimtron

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

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


佚名

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


我知道 System.Web.UI.Pair。虽然想要通用类。
System.Web.UI.Pair 是密封的。您不能从中派生(如果您想添加类型安全访问器)。
S
Serge Mikhailov

从 .NET 4.0 开始,您有 System.Tuple<T1, T2> 类:

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

@Alexander,您可以轻松查看 .NET 3.5 docs on Tuple
在底部他们说:版本信息 NET Framework 支持:4
@Alexander:好的,对。 (虽然这让我想知道为什么他们让这个页面特定于 .NET 3.5)
p
parliament

我通常将 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);

A
Andrew Stein

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

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

抑制编译器警告。


您应该找到比这更好的哈希码算法,尝试使用 37+23*(h1+23*(h2+23*(h3+...))) 这将使 (A,B) 不同于 (B,A ), IE。重新排序将对代码产生影响。
评论已被接受。在我的情况下,我只是想抑制编译器减弱,无论如何 T 是一个字符串,U 是一个 Int32 ......
J
James Webster

PowerCollections 库(以前可从 Wintellect 获得,但现在托管在 Codeplex @ http://powercollections.codeplex.com 上)具有通用的 Pair 结构。