什么是 C++ 的 C# 类似物? 标准::对?
C# 中 C++ 中 std::pair
的模拟是什么? 我找到了 System.Web.UI.Pair 类,但我更喜欢基于模板的类。
What is a C#'s analog of std::pair
in C++? I found System.Web.UI.Pair
class, but I'd prefer something template-based.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
元组自 .NET4.0 起可用 并支持泛型:
在以前的版本中,您可以使用
System.Collections.Generic.KeyValuePair
或如下所示的解决方案:并像这样使用它:
此输出:
或者甚至是这样链对:
输出:
Tuples are available since .NET4.0 and support generics:
In previous versions you can use
System.Collections.Generic.KeyValuePair<K, V>
or a solution like the following:And use it like this:
This outputs:
Or even this chained pairs:
That outputs:
System.Web.UI
包含Pair
类,因为它在 ASP.NET 1.1 中被大量用作内部 ViewState 结构。2017 年 8 月更新: C# 7.0 / .NET Framework 4.7 提供了使用
System.ValueTuple
结构。有关更多语法示例,请参阅 MSDN。
2012 年 6 月更新:
元组
自 4.0 版本以来一直是 .NET 的一部分。
这是一篇早期文章,描述了 .NET4.0 中的包含内容以及对泛型:
System.Web.UI
contained thePair
class because it was used heavily in ASP.NET 1.1 as an internal ViewState structure.Update Aug 2017: C# 7.0 / .NET Framework 4.7 provides a syntax to declare a Tuple with named items using the
System.ValueTuple
struct.see MSDN for more syntax examples.
Update Jun 2012:
Tuples
have been a part of .NET since version 4.0.Here is an earlier article describing inclusion in.NET4.0 and support for generics:
不幸的是,没有。 您可以在许多情况下使用
System.Collections.Generic.KeyValuePair
。或者,您可以使用匿名类型来处理元组,至少在本地:
最后一种选择是创建一个自己的类。
Unfortunately, there is none. You can use the
System.Collections.Generic.KeyValuePair<K, V>
in many situations.Alternatively, you can use anonymous types to handle tuples, at least locally:
The last alternative is to create an own class.
从 4.0 版开始,C# 具有元组。
C# has tuples as of version 4.0.
有些答案似乎是错误的,
这是我的对类
这是一些测试代码:
Some answers seem just wrong,
Here is my pair class
Here is some test code:
除了自定义类或 .Net 4.0 元组之外,从 C# 7.0 开始,还有一个名为 ValueTuple 的新功能,它是一个可以在这种情况下使用的结构。 您可以简单地这样做:
通过 t.Item1 和 t.Item2 访问值
甚至可以: 而不是
Apart from custom class or .Net 4.0 Tuples, since C# 7.0 there is a new feature called ValueTuple, which is a struct that can be used in this case. Instead of writing:
and access values through
t.Item1
andt.Item2
, you can simply do it like that:or even:
如果它与字典等有关,您正在寻找 System.Collections.Generic.KeyValuePair。
If it's about dictionaries and the like, you're looking for System.Collections.Generic.KeyValuePair<TKey, TValue>.
我创建了元组的 C# 实现,它一般解决了两到五个值之间的问题 - 这是博客文章,其中包含源链接。
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.
我通常将
Tuple
类扩展为我自己的通用包装器,如下所示:并像这样使用它:
I typically extend the
Tuple
class into my own generic wrapper as follows:and use it like so:
根据您想要完成的任务,您可能需要尝试 KeyValuePair。
您无法更改条目的键的事实当然可以通过简单地用 KeyValuePair 的新实例替换整个条目来纠正。
Depending on what you want to accomplish, you might want to try out 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.
我刚刚在快速谷歌后问了同样的问题,我发现 .NET 中有一个对类,除了它在 System.Web.UI ^ ~ ^ (http://msdn.microsoft.com/en-us/library/system.web.ui.pair.aspx)
天知道为什么他们把它放在那里而不是集合框架
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 4.0 开始,您有
System.Tuple
类:Since .NET 4.0 you have
System.Tuple<T1, T2>
class:为了使上述工作正常(我需要一对作为字典的键)。 我必须添加:
一旦我这样做了,我还添加了
抑制编译器警告的内容。
On order to get the above to work (I needed a pair as the key of a dictionary). I had to add:
and once I did that I also added
to suppress a compiler warning.
PowerCollections 库(以前可从 Wintellect 获取,但现在托管在 Codeplex @ http://powercollections.codeplex.com 上)具有通用的 Pair 结构。
The PowerCollections library (formerly available from Wintellect but now hosted on Codeplex @ http://powercollections.codeplex.com) has a generic Pair structure.