C# Array initialization - with non-default value

前端 未结 6 1808
温柔的废话
温柔的废话 2020-12-08 20:03

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[] GetPageN         


        
6条回答
  •  醉梦人生
    2020-12-08 20:29

    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.

提交回复
热议问题