数组数组呢?如果您发现您经常需要替换 1 整行的内容,那么也许每一行本身应该是一个数组。您甚至可以定义自己的包含行数据的类,然后定义该类的数组。
顺便说一句,你为什么有 ToList() 和 ToArray()?
编辑:假设结果必须是二维数组,我将只使用下面的 foreach 版本:
这样定义 ForEach(这是我最喜欢的扩展方法):
public static IEnumerable<TSource> ForEach<TSource>(this System.Collections.Generic.IEnumerable<TSource> source, Action<TSource> action)
{
ThrowIfNull(source, "source");
ThrowIfNull(action, "action");
foreach (TSource item in source)
{
action(item);
}
return source;
}
public static IEnumerable<TSource> ForEach<TSource>(this System.Collections.Generic.IEnumerable<TSource> source, Action<TSource, int> action)
{
ThrowIfNull(source, "source");
ThrowIfNull(action, "action");
int index = 0;
foreach (TSource item in source)
{
action(item, index);
index++;
}
return source;
}
那就做吧
this.Facts.Select(f =>f.FactIc).ForEach((f, i) => vals[1, i] = f);
没有比这更有效的了。开始时数据不在平面数组中,因此您需要以某种方式迭代每个项目并复制数据。这样可以避免制作数组的中间副本等。