-3

I'm looking for the equivalent of Array.Fill but for List

Is it possible to fill all list items with certain value

Array.Fill(counters, max); // this works

listName.Fill(5);   //something like this

would fill a list with 5's

I don't want to use a loop

Given that the list have some items

Adam Bahrani
  • 65
  • 3
  • 11
  • Whats wrong with a for loop? or make an extension method? Either-way something has to loop – TheGeneral Jul 13 '19 at 23:33
  • Loop would cause more run time – Adam Bahrani Jul 13 '19 at 23:35
  • [`List.ForEach`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.foreach)? – 41686d6564 stands w. Palestine Jul 13 '19 at 23:35
  • Every possible solution will loop whether you like it or not, so i would get that out of your mind. Now that we know this, we just need to choose a looping solution – TheGeneral Jul 13 '19 at 23:35
  • That's a loop @AhmedAbdelhameed – Adam Bahrani Jul 13 '19 at 23:36
  • One could wonder you would want a list of all the same values anyway... – RobIII Jul 13 '19 at 23:38
  • 1
    I thought you just wanted a one-liner. You should know that at the low-level, everything (that is repetitive) gets converted to loops. You same to have a very wrong idea about loops. – 41686d6564 stands w. Palestine Jul 13 '19 at 23:38
  • There is no way to magic data into memory, Memory is a physical device, and needs to be prodded to hold a value, even memset in C loops, take a look at the source here https://code.woboq.org/gcc/libgcc/memset.c.html – TheGeneral Jul 13 '19 at 23:40
  • There's a reason for array.fill method @AhmedAbdelhameed – Adam Bahrani Jul 13 '19 at 23:41
  • What exact issue do you have that you are trying to solve with this micro-optimization? The runtime of a loop is in most cases negligible even with a million entries. – ckuri Jul 13 '19 at 23:47
  • 3
    @AdamBahrani Yes, there's a reason for `Array.Fill` but it's not what you think. It's to save developers' time, not to increase performance. `Array.Fill()` just uses a loop to iterate and set the elements ([here's the source code](https://source.dot.net/#System.Private.CoreLib/shared/System/Array.cs,645); you can check it yourself). You have to use a loop one way or another; there's no way around that. Like I said above, you have a very wrong idea about loops. – 41686d6564 stands w. Palestine Jul 13 '19 at 23:50
  • Thanks for that Refernce @AhmedAbdelhameed, when I used fill had slight improvement at run time with array, that's why, but agreed on the basics – Adam Bahrani Jul 13 '19 at 23:55
  • @AdamBahrani _"when I used fill had slight improvement"_ I really hate to use definitive statements but that's basically impossible! (Unless someone can correct me). – 41686d6564 stands w. Palestine Jul 13 '19 at 23:59

1 Answers1

2

Enumerable.Repeat(TResult, Int32) Method

var fives = Enumerable.Repeat(5, 10).ToList(); // create list with ten fives

You need to loop all items for existing list anyway, just wrap the loop with an extension method.

public static void FillWith<T>(this List<T> list, T value)
{
    for (var i = 0, i < list.Count, i++)
    {
        list[i] = value;
    }
}

Usage

var list = new List<int> { 1, 2, 3, 4, 5 };
list.FillWith(42);

var output string.Join(",", list); // 42,42,42,42,42    
Fabio
  • 31,528
  • 4
  • 33
  • 72
  • 1
    up voting as this is something inline with what I was looking for, but thought there was a way to do it without iteration, just one. – Adam Bahrani Jul 13 '19 at 23:50