An extension method to fill all indices of an IList with a default value. A part of the C# Language Syntactic Sugar suite.
$ dotnet add package CLSS.ExtensionMethods.IList.FillByArray.Fill is a convenient method to fill an array first introduced in .NET Standard 2.1. It has the following drawbacks:
This package provides FillBy to all IList<T> types for .NET Standard 1.0 & 2.0. It rectified the above drawbacks.
Filling a List<T> with a factory function, passing through the element's index number:
using CLSS;
using System.Linq;
var numbers = Enumerable.Repeat(0, 5).ToList();
numbers.FillBy(i => i * i); // { 0, 1, 4, 9, 16, }
FillBy returns source collection, saving some lines of code and functional-style friendly:
using CLSS;
using System.Linq;
// Create and fill array the standard way
var numbers1 = new int[5];
Array.Fill(numbers1, 4);
// Create and fill array with FillBy
var numbers2 = new int[5].FillBy(4);
// Easy functional syntax
bool allValid = new int[5].FillBy(FactoryFunction)
.Append(existingResults)
.All(VerifyResult);
Note: ExclusiveSample works on all types implementing the IList<T> interface, including raw C# array.