Direct array initialization with a constant value

前端 未结 6 1551
心在旅途
心在旅途 2020-12-09 14:44

Whenever you allocate a new array in C# with

new T[length]

the array entries are set to the default of T. That is null for th

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 14:52

    I suggest using Array.Fill as a very succint way to fill an array with an initial value:

    bool[] isPrime = new bool[MaxNum];
    Array.Fill(isPrime, true);
    

    This initializes all values in the isPrime array to true.

提交回复
热议问题