您知道在 C# 中初始化动态大小数组的最巧妙方法是什么?
这是我能想到的最好的
private bool[] GetPageNumbersToLink(IPagedResult result)
{
if (result.TotalPages <= 9)
return new bool[result.TotalPages + 1].Select(b => true).ToArray();
...
最佳答案
如果“最流畅”是指最快,恐怕 Enumerable.Repeat 可能比 for 循环慢 20 倍。 参见 http://dotnetperls.com/initialize-array :
Initialize with for loop: 85 ms [much faster]
Initialize with Enumerable.Repeat: 1645 ms
所以使用 Dotnetguy 的 SetAllValues() 方法。
关于C# 数组初始化 - 具有非默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/136836/