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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/166089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:25:48  来源:igfitidea点击:

What is C# analog of C++ std::pair?

c#.netdata-structuresstd-pairbase-class-library

提问by Alexander Prokofyev

I'm interested: What is C#'s analog of std::pairin C++? I found System.Web.UI.Pairclass, but I'd prefer something template-based.

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

Thank you!

谢谢!

采纳答案by Jorge Ferreira

Tuples are available since .NET4.0and support generics:

自 .NET4.0 起元组可用并支持泛型:

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


In previous versions you can use System.Collections.Generic.KeyValuePair<K, V>or a solution like the following:

在以前的版本中,您可以使用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; }
};

And use it like this:

并像这样使用它:

Pair<String, int> pair = new Pair<String, int>("test", 2);
Console.WriteLine(pair.First);
Console.WriteLine(pair.Second);

This outputs:

这输出:

test
2

Or even this chained pairs:

甚至这个链对:

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

That outputs:

输出:

test
12
true

回答by OregonGhost

If it's about dictionaries and the like, you're looking for System.Collections.Generic.KeyValuePair<TKey, TValue>.

如果是关于字典之类的,你正在寻找 System.Collections.Generic.KeyValuePair<TKey, TValue>。

回答by Konrad Rudolph

Unfortunately, there is none. You can use the System.Collections.Generic.KeyValuePair<K, V>in many situations.

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

Alternatively, you can use anonymous types to handle tuples, at least locally:

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

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

The last alternative is to create an own class.

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

回答by Grimtron

Depending on what you want to accomplish, you might want to try out KeyValuePair.

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

The fact that you cannot change the key of an entry can of course be rectified by simply replacing the entire entry by a new instance of KeyValuePair.

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

回答by Grimtron

I was asking the same question just now after a quick google I found that There is a pair class in .NET except its in the System.Web.UI ^ ~ ^ (http://msdn.microsoft.com/en-us/library/system.web.ui.pair.aspx) goodness knows why they put it there instead of the collections framework

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

回答by Erik Forbes

I created a C# implementation of Tuples, which solves the problem generically for between two and five values - here's the blog post, which contains a link to the source.

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

回答by Jay Walker

System.Web.UIcontained the Pairclass because it was used heavily in ASP.NET 1.1 as an internal ViewState structure.

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

Update Aug 2017:C# 7.0 / .NET Framework 4.7 provides a syntax to declare a Tuple with named items using the System.ValueTuplestruct.

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

see MSDNfor more syntax examples.

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

Update Jun 2012:Tupleshave been a part of .NET since version 4.0.

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

Here is an earlier article describing inclusion in.NET4.0and support for generics:

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

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

回答by Kenan E. K.

C# has tuplesas of version 4.0.

从 4.0 版开始,C# 具有元组

回答by Andrew Stein

On order to get the above to work (I needed a pair as the key of a dictionary). I had to add:

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

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

and once I did that I also added

一旦我这样做了,我还添加了

    public override Int32 GetHashCode()
    {
        return First.GetHashCode() ^ Second.GetHashCode();
    }

to suppress a compiler warning.

抑制编译器警告。

回答by James Webster

The PowerCollections library (formerly available from Wintellect but now hosted on Codeplex @ http://powercollections.codeplex.com) has a generic Pair structure.

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