c# - Set same value to an array/list without using a for loop -
i want set same value specific range of array or list without using loop. follows:
int[] myarray = new int[100]; int lower = 20; int upper = 80; int value = 5; myarray.method(lower,upper,value);
i have tried myarray.setvalue()
, mylist.insertrange()
allows set 1 value, not range.
is c#-method doing task? done without loop?
well, create temporary array in right size , copy source array
array.copy(enumerable.repeat(value, upper-lower+1).toarray(), 0, myarray, lower, upper-lower+1);
but that's inefficient (and, internally, enumerable.repeat
uses loop, too)
i don't see what's wrong simple for
loop:
for(int = lower; <= upper; i++) myarray[i] = value;
Comments
Post a Comment