7

I'm using the following statement in Java:

Arrays.fill(mynewArray, oldArray.Length, size, -1);

Please suggest the C# equivalent.

Henrik Heimbuerger
  • 9,924
  • 6
  • 56
  • 69
usr021986
  • 3,421
  • 14
  • 53
  • 64
  • https://learn.microsoft.com/en-us/dotnet/api/system.array.fill?view=netcore-3.1, also dotnet provide the same function. – Jiaji Li Sep 19 '20 at 17:15

3 Answers3

10

I don't know of anything in the framework which does that, but it's easy enough to implement:

// Note: start is inclusive, end is exclusive (as is conventional
// in computer science)
public static void Fill<T>(T[] array, int start, int end, T value)
{
    if (array == null)
    {
        throw new ArgumentNullException("array");
    }
    if (start < 0 || start >= end)
    {
        throw new ArgumentOutOfRangeException("fromIndex");
    }
    if (end >= array.Length)
    {
        throw new ArgumentOutOfRangeException("toIndex");
    }
    for (int i = start; i < end; i++)
    {
        array[i] = value;
    }
}

Or if you want to specify the count instead of the start/end:

public static void Fill<T>(T[] array, int start, int count, T value)
{
    if (array == null)
    {
        throw new ArgumentNullException("array");
    }
    if (count < 0)
    {
        throw new ArgumentOutOfRangeException("count");
    }
    if (start + count >= array.Length)
    {
        throw new ArgumentOutOfRangeException("count");
    }
    for (var i = start; i < start + count; i++)
    {
        array[i] = value;
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I hope my edit is justified.. or otherwise name variable to `uptoIndex` – nawfal Nov 15 '12 at 23:27
  • @nawfal: No, it was deliberately (although not consistently) exclusive. I've tidied it up and added an alternative. – Jon Skeet Nov 16 '12 at 06:47
  • Jon, why do you check for exceptional cases and throw yourself when the clr would do that? I'm a bit puzzled by this kind of design. Wouldn't only the second conditional check (in both your examples) alone suffice? – nawfal Nov 20 '12 at 09:24
  • 1
    @nawfal: The CLR will throw different exceptions. Given that these are method arguments, the exceptions thrown should be ArgumentExceptions, IMO. That shows that it's the calling code at fault, not a problem within the method itself. – Jon Skeet Nov 20 '12 at 09:39
  • @JonSkeet, it is a little bit too late... still a thought: negative count will not cause an exception in the loop... throwing on negative count or on inverted start and stop needs to be well justified... – dmitry Mar 13 '17 at 14:55
2

It seems like you would like to do something more like this

int[] bar = new int[] { 1, 2, 3, 4, 5 };
int newSize = 10;
int[] foo = Enumerable.Range(0, newSize).Select(i => i < bar.Length ? bar[i] : -1).ToArray();

Creating an new larger array with the old values and filling the extra.

For a simple fill try

int[] foo = Enumerable.Range(0, 10).Select(i => -1).ToArray();

or a sub range

int[] foo = new int[10];
Enumerable.Range(5, 9).Select(i => foo[i] = -1);
CCondron
  • 1,926
  • 17
  • 27
-2

Try like this

Array.Copy(source, target, 5);

For more information here

Rasel
  • 15,499
  • 6
  • 40
  • 50