【发布时间】:2017-08-24 20:55:50
【问题描述】:
我希望动态创建具有相同类型的给定大小的元组。
所以如果我想要一个大小为 3 的字符串元组,我会得到 Tuple
我已经尝试将字符串传递到 Tuple 的尖括号中,如下所示:
string foo = "string, string, string";
Tuple<foo> testTuple = Tuple<foo>(~parameters~);
并将类型数组传递到尖括号中,如下所示:
List<Type> types = new List<Type>() {"".GetType(), "".GetType(), "".GetType()};
Tuple<types> testTuple = Tuple<foo>(~parameters~);
这些都不起作用。有谁知道如何制作所描述的动态元组?
(我想这样做的原因是在像
这样的字典中使用元组Dictionary<Tuple<# strings>, int> testDictionary = new Dictionary<Tuple<x # strings>, int>();
在这里使用元组比 HashSets 更有用,因为元组中的比较是通过组件而不是通过引用,所以如果我有
Tuple<string, string> testTuple1 = new Tuple<string, string>("yes", "no");
Tuple<string, string> testTuple2 = new Tuple<string, string>("yes", "no");
Dictionary<Tuple<string, string>, string> testDictionary = new Dictionary<Tuple<string, string>, string>() {
{testTuple1, "maybe"}
};
Console.WriteLine(testDict[testTuple2]);
它写“也许”。如果您使用 HashSets 运行相同的测试,则会引发错误。如果有更好的方法来完成同样的事情,那也会很有用。)
【问题讨论】:
-
您似乎有一些外部需求,您正试图将它们塞进框架类型(元组、字典)中。别。创建自定义类型,以您需要的方式保存您需要的数据。
标签: c# dictionary tuples