【发布时间】:2017-09-03 02:08:10
【问题描述】:
C# 7.0(在 VS 2017 中)中的新功能是否可以将元组字段名称转换为 KeyValuePairs?
假设我有这个:
class Entry
{
public string SomeProperty { get; set; }
}
var allEntries = new Dictionary<int, List<Entry>>();
// adding some keys with some lists of Entry
最好能做这样的事情:
foreach ((int collectionId, List<Entry> entries) in allEntries)
我已经在项目中添加了System.ValueTuple。
能这样写就比这种传统风格好多了:
foreach (var kvp in allEntries)
{
int collectionId = kvp.Key;
List<Entry> entries = kvp.Value;
}
【问题讨论】:
-
请提供minimal reproducible example。我们不知道
allEntries是什么,这让我们很难提供帮助...... -
@JonSkeet 我添加了更多关于这可能是哪种字典的数据,尽管问题很笼统,也可能是
Dictionary<int, string>。 -
只是
Dictionary<,>的事实是一个好的开始 - 你之前没有提到过。我认为如果您使用Dictionary<int, string>用minimal reproducible example 重写问题会更简单。我一会儿看看……
标签: c# dictionary c#-7.0 valuetuple