在 C# 中使用一对(三重等)值作为一个值的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/101825/
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 14:04:35  来源:igfitidea点击:

What's the best way of using a pair (triple, etc) of values as one value in C#?

提问by Max Galkin

That is, I'd like to have a tuple of values.

也就是说,我想要一个值元组。

The use case on my mind:

我想到的用例:

Dictionary<Pair<string, int>, object>

or

或者

Dictionary<Triple<string, int, int>, object>

Are there built-in types like Pair or Triple? Or what's the best way of implementing it?

是否有像 Pair 或 Triple 这样的内置类型?或者实现它的最佳方法是什么?

UpdateThere are some general-purpose tuples implementations described in the answers, but for tuples used as keys in dictionaries you should additionaly verify correct calculation of the hash code. Some more info on that in another question.

更新答案中描述了一些通用元组实现,但是对于用作字典中键的元组,您应该另外验证哈希码的正确计算。关于另一个问题的更多信息。

Update 2I guess it is also worth reminding, that when you use some value as a key in dictionary, it should be immutable.

更新 2我想还值得提醒的是,当您使用某个值作为字典中的键时,它应该是不可变的。

采纳答案by Michael L Perry

I have implemented a tuple library in C#. Visit http://www.adventuresinsoftware.com/generics/and click on the "tuples" link.

我已经在 C# 中实现了一个元组库。访问http://www.adventuresinsoftware.com/generics/并单击“元组”链接。

回答by Grad van Horck

I usually just create my own struct, containing the values. It's often a bit more readable ;)

我通常只是创建自己的结构,其中包含值。它通常更具可读性;)

回答by Grad van Horck

There aren't built ins, but a Pair<T,R> class is trivial to create.

没有内置插件,但创建 Pair<T,R> 类很简单。

回答by Adam Vigh

Pair and Triplet are existing classes in .net see msdn:

Pair 和 Triplet 是 .net 中的现有类,请参见 msdn:

Triplet

三胞胎

Pair

一对

I recently came across them, while playing around with viewstate decoding

我最近在玩视图状态解码时遇到了它们

回答by Timothy Carter

public struct Pair<T1, T2>
{
    public T1 First;
    public T2 Second;
}

public struct Triple<T1, T2, T3>
{
    public T1 First;
    public T2 Second;
    public T3 Third;
}

回答by ballpointpeon

Aye, there's System.Web.UI.Pair and System.Web.UI.Triplet (which has an overloaded creator for Pair-type behaviour!)

是的,有 System.Web.UI.Pair 和 System.Web.UI.Triplet(它有一个用于 Pair 类型行为的重载创建者!)

回答by Jonas Lincoln

For the first case, I usually use

对于第一种情况,我通常使用

Dictionary<KeyValuePair<string, int>, object>

回答by aku

There are not built-in classes for that. You can use KeyValuePairor roll out your own implementation.

没有内置的类。您可以使用KeyValuePair或推出您自己的实现。

回答by Amy B

You could use System.Collections.Generic.KeyValuePair as your Pair implementation.

您可以使用 System.Collections.Generic.KeyValuePair 作为您的 Pair 实现。

Or you could just implement your own, they aren't hard:

或者你可以自己实现,它们并不难:

public class Triple<T, U, V>
{
  public T First {get;set;}
  public U Second {get;set;}
  public V Third {get;set;}
}

Of course, you may someday run into a problem that Triple(string, int, int) is not compatible with Triple(int, int, string). Maybe go with System.Xml.Linq.XElement instead.

当然,有一天你可能会遇到 Triple(string, int, int) 与 Triple(int, int, string) 不兼容的问题。也许改用 System.Xml.Linq.XElement 。

回答by TraumaPony

There are also the Tuple<> types in F#; you just need to reference FSharp.Core.dll.

F# 中也有 Tuple<> 类型;你只需要引用 FSharp.Core.dll。