什么';在C#中使用一对(三元组等)值作为一个值的最佳方法是什么?
也就是说,我想要一个值的元组 我想到的用例是:什么';在C#中使用一对(三元组等)值作为一个值的最佳方法是什么?,c#,generics,data-structures,tuples,C#,Generics,Data Structures,Tuples,也就是说,我想要一个值的元组 我想到的用例是: Dictionary<Pair<string, int>, object> 字典 或 字典 是否有内置类型,如配对或三重?或者最好的实施方式是什么 更新答案中描述了一些通用元组实现,但对于用作字典中键的元组,您还应验证哈希代码的正确计算。在另一个网站上有更多的信息 更新2我想这也值得提醒一下,当您在字典中使用某个值作为键时,它应该是不可变的。我通常只创建自己的结构,包含这些值。它通常更具可读性;) 没有内置类,但是创建
Dictionary<Pair<string, int>, object>
字典
或
字典
是否有内置类型,如配对或三重?或者最好的实施方式是什么
更新答案中描述了一些通用元组实现,但对于用作字典中键的元组,您还应验证哈希代码的正确计算。在另一个网站上有更多的信息
更新2我想这也值得提醒一下,当您在字典中使用某个值作为键时,它应该是不可变的。我通常只创建自己的结构,包含这些值。它通常更具可读性;) 没有内置类,但是创建Pair类非常简单 Pair和Triplet是.net中现有的类,请参见msdn: 我最近在玩viewstate解码时遇到了它们
公共结构对
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;
}
{
公共T1优先;
公众T2秒;
}
公共结构三元组
{
公共T1优先;
公众T2秒;
公共交通;
}
是的,有System.Web.UI.Pair和System.Web.UI.Triplet(它有一个重载的对类型行为的创建者!)对于第一种情况,我通常使用
Dictionary<KeyValuePair<string, int>, object>
字典
没有用于此的内置类。您可以使用或推出自己的实现。您可以使用System.Collections.Generic.KeyValuePair作为您的Pair实现
或者你可以自己实现,它们并不难:
public class Triple<T, U, V>
{
public T First {get;set;}
public U Second {get;set;}
public V Third {get;set;}
}
公共类三元组
{
公共T第一个{get;set;}
公共U秒{get;set;}
公共V第三{get;set;}
}
当然,有一天您可能会遇到这样一个问题:Triple(string,int,int)与Triple(int,int,string)不兼容。也许可以改为使用System.Xml.Linq.XElement。我在C#中实现了一个元组库。访问并单击“元组”链接。F#中也有元组类型;您只需要引用FSharp.Core.dll。如果不想创建自己的类,那么KeyValuePair是最好的扩展类
int id = 33;
string description = "This is a custom solution";
DateTime created = DateTime.Now;
KeyValuePair<int, KeyValuePair<string, DateTime>> triple =
new KeyValuePair<int, KeyValuePair<string, DateTime>>();
triple.Key = id;
triple.Value.Key = description;
triple.Value.Value = created;
intid=33;
string description=“这是一个自定义解决方案”;
DateTime created=DateTime.Now;
键值对三元组=
新的KeyValuePair();
三重密钥=id;
triple.Value.Key=描述;
triple.Value.Value=已创建;
您可以根据需要将其扩展到任意多个级别
KeyValuePair<KeyValuePair<KeyValuePair<string, string>, string, string> quadruple =
new KeyValuePair<KeyValuePair<KeyValuePair<string, string>, string, string>();
KeyValuePair内置类
在某些特定情况下,.net framework已经提供了类似元组的类,您可以利用这些类
对和三元组
泛型
类可以用作临时对实现。这是一个
通用词典在内部使用
或者,您也可以与
作为基本对的结构,具有
在mscorlib中可用。然而,不利的一面是,这一结构并非如此
强类型
成对和三元组也可以以
和
课程。即使这些类存在于System.Web程序集中
它们可能非常适合winforms开发。但是,这些类是
也不是强类型的,并且可能不适用于某些场景,例如通用框架或库
高阶元组
对于高阶元组,除了滚动您自己的类之外,可能还有
这不是一个简单的解决方案
如果您已安装
,您可以引用包含一组通用不可变参数的FSharp.Core.dll
课程
最多可达通用六元组。但是,即使未修改的FSharp.Code.dll
可以重新分配,F#是一种研究语言,也是一项正在进行的工作
这一解决方案可能只引起学术界的兴趣
如果您不想创建自己的类并且感到不舒服
引用F#库时,一个巧妙的技巧可能包括扩展泛型KeyValuePair类,以便值成员本身就是嵌套的KeyValuePair
例如,下面的代码说明了如何利用
要创建三元组,请使用KeyValuePair:
int id = 33;
string description = "This is a custom solution";
DateTime created = DateTime.Now;
KeyValuePair<int, KeyValuePair<string, DateTime>> triple =
new KeyValuePair<int, KeyValuePair<string, DateTime>>();
triple.Key = id;
triple.Value.Key = description;
triple.Value.Value = created;
intid=33;
string description=“这是一个自定义解决方案”;
DateTime created=DateTime.Now;
键值对三元组=
新的KeyValuePair();
三重密钥=id;
triple.Value.Key=描述;
triple.Value.Value=已创建;
这允许根据需要将类扩展到任意级别
KeyValuePair<KeyValuePair<KeyValuePair<string, string>, string>, string> quadruple =
new KeyValuePair<KeyValuePair<KeyValuePair<string, string>, string>, string>();
KeyValuePair<KeyValuePair<KeyValuePair<KeyValuePair<string, string>, string>, string>, string> quintuple =
new KeyValuePair<KeyValuePair<KeyValuePair<KeyValuePair<string, string>, string>, string>, string>();
KeyValuePair四重=
新的KeyValuePair();
KeyValuePair五元组=
新的KeyValuePair();
自己动手
在其他情况下,您可能需要自己滚动
tuple类,这并不难
您可以创建如下简单结构:
struct Pair<T, R>
{
private T first_;
private R second_;
public T First
{
get { return first_; }
set { first_ = value; }
}
public R Second
{
get { return second_; }
set { second_ = value; }
}
}
结构对
{
私人T优先;
私人住宅;
公众优先
{
获取{返回第一个}
设置{first\=value;}
}
公共R秒
{
获取{返回第二个}
设置{second\=value;}
}
}
框架和库
这个问题在以前和通用框架中已经解决过
确实存在。以下是一个此类框架的链接:
- 借
您可以相对轻松地创建自己的元组类,唯一可能变得混乱的是等式和哈希代码重写(如果要在字典中使用它们,则必须使用它们)
应该注意.Net自己的KeyValuePair
struct
假设这不是您所关心的问题,那么代码最终还是很难解决的问题:
public Tuple<int, string, int> GetSomething()
{
//do stuff to get your multi-value return
}
//then call it:
var retVal = GetSomething();
//problem is what does this mean?
retVal.Item1 / retVal.Item3;
//what are item 1 and 3?
NGenerics-流行的.Net算法和数据结构库,最近引入了不可变数据
public Tuple<int, string, int> GetSomething()
{
//do stuff to get your multi-value return
}
//then call it:
var retVal = GetSomething();
//problem is what does this mean?
retVal.Item1 / retVal.Item3;
//what are item 1 and 3?
class CustomRetVal {
int CurrentIndex { get; set; }
string Message { get; set; }
int CurrentTotal { get; set; }
}
var retVal = GetSomething();
//get % progress
retVal.CurrentIndex / retVal.CurrentTotal;