What is the slickest way to initialize an array of dynamic size in C# that you know of?
This is the best I could come up with
private bool[] GetPageNumbersToLink(IPagedResult result)
{
if (result.TotalPages <= 9)
return new bool[result.TotalPages + 1].Select(b => true).ToArray();
...
If by 'slickest' you mean fastest, I'm afraid that Enumerable.Repeat may be 20x slower than a for loop. See http://dotnetperls.com/initialize-array:
Initialize with for loop: 85 ms [much faster]
Initialize with Enumerable.Repeat: 1645 ms
So use Dotnetguy's SetAllValues() method.
use Enumerable.Repeat
Enumerable.Repeat(true, result.TotalPages + 1).ToArray()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With