26

In Java there are about 18 a static "fill" methods in the class Arrays that serve to assign one value to each element in an array. I'm looking for an equivalent in C# to achieve the same thing but I'm not finding anything with the same compactness:

1) ForEach as I understand passes elements in the array by value so I can't change them

2) Repeat from Enumerable creates a new array, it seems to be overhead to create a new array and then copy each element from the array

3) a for-loop isn't pretty, which I guess is why the Java folks introduced this compact notation to begin with

4) The Clear method from the Array class can set everything to zero, but then how do I convert zero to the non-zero value I want?

To demonstrate Java's syntax consider this code that prints three times the number 7:

int[] iii = {1, 2, 3};
Arrays.fill(iii, 7);
for (int i : iii) {
    System.out.println(i);
}
Mishax
  • 4,442
  • 5
  • 39
  • 63

6 Answers6

66
int[] arr = Enumerable.Repeat(42, 10000).ToArray();

I believe that this does the job :)

Alex Shtoff
  • 2,520
  • 1
  • 25
  • 53
  • 4
    I was aware of this method as I mentioned in the OP, I was looking for something that modified an existing array. – Mishax Feb 19 '13 at 16:01
21

Write yourself an extension method

public static class ArrayExtensions {
    public static void Fill<T>(this T[] originalArray, T with) {
        for(int i = 0; i < originalArray.Length; i++){
            originalArray[i] = with;
        }
    }  
}

and use it like

int foo[] = new int[]{0,0,0,0,0};
foo.Fill(13);

will fill all the elements with 13

Display Name
  • 8,022
  • 3
  • 31
  • 66
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
13

You could try something like this:

I have initialzed the array for having value 5, you could put your number similarly.

int[] arr = new int[10]; // your initial array

arr = arr.Select(i => 5).ToArray(); // array initialized to 5.
Igoy
  • 2,942
  • 22
  • 23
5

Say you want to fill with number 13.

int[] myarr = Enumerable.Range(0, 10).Select(n => 13).ToArray();

or

List<int> myarr = Enumerable.Range(0,10).Select(n => 13).ToList();

if you prefer a list.

roadrunner66
  • 7,772
  • 4
  • 32
  • 38
  • isn't this creating a new array? Arrays.fill() is not doing that in Java, it is modifying an existing one. – Mishax Feb 19 '13 at 16:02
  • Since the question was about filling an array with a constant value (i.e. just for initialization), I assumed that you were creating the array right there and then. So I tried to provide a short answer. If your array already existed before, I'd use the Select method in Igoy's answer. – roadrunner66 Feb 20 '13 at 18:52
  • I like this answer since it gives me the opportunity to choose the index range in which the constant will be injected. +1! – Gergely Lukacsy Sep 27 '16 at 14:56
  • 1
    When using EF, I love the list idea, as it is easy to work with and already in common usage for many other entities and objects. +1 here – ransems Sep 13 '18 at 15:24
4

you could write an extension method

public static void Fill<T>(this T[] array, T value)
{
  for(int i = 0; i < array.Length; i++) 
  {
     array[i] = value;
  }
}
WannaCSharp
  • 1,898
  • 2
  • 13
  • 19
3
public static void Fill<T>(this IList<T> col, T value, int fromIndex, int toIndex)
{
    if (fromIndex > toIndex)
        throw new ArgumentOutOfRangeException("fromIndex");

    for (var i = fromIndex; i <= toIndex; i++)
        col[i] = value;
}

Something that works for all IList<T>s.

nawfal
  • 70,104
  • 56
  • 326
  • 368
  • 1
    `IList` is still `T[]` – nawfal Feb 17 '13 at 08:43
  • If such a generic function - I'd use default prms... public static void Fill(this IList col, T value, int fromIndex=0, int toIndex=-1) { if (toIndex == -1) toIndex = col.Count-1; – ephraim Jul 15 '19 at 11:06