I found this code snippet on a different post that I don’t quite understand and would appriciate someone explaining.
private bool[] GetPageNumbersToLink(IPagedResult result)
{
if (result.TotalPages <= 9)
return new bool[result.TotalPages + 1].Select(b => true).ToArray();
...
C# Array initialization – with non-default value
My take on this:
new bool[result.TotalPages + 1]
this creates an array of bools with length equal to that of the totalpages, the default value being false on all of them.
.Select(b => true)
this uses a linq query to return only the items that are true, i.e. none of them
.ToArray();
linq returns IEnumerable or something, so it needs to go back to a new array, of length 0 as none were selected in the first place.
I think that’s what it’s doing, but it doesn’t make any sense.
(EDIT: Oops, misread the post (unless it was edited without me noticing).)
No, that’s not what it’s doing. The result is effectively:
The
Selectcall doesn’t depend on the value that it’s selecting at all – it’s just returningtruewhatever the input is.Selectdoesn’t perform filtering, just projection.In other words:
Anyway it’s inefficient. It’s occasionally unfortunate that .NET doesn’t provide something like: