C# – a equivalent of Delphi FillChar in C#


What is the C# equivalent of Delphi's FillChar?

Best Solution

If I understand FillChar correctly, it sets all elements of an array to the same value, yes?

In which case, unless the value is 0, you probably have to loop:

for(int i = 0 ; i < arr.Length ; i++) {
    arr[i] = value;

For setting the values to the type's 0, there is Array.Clear

Obviously, with the loop answer you can stick this code in a utility method if you need... for example, as an extension method:

public static void FillChar<T>(this T[] arr, T value) {...}

Then you can use:

int[] data = {1,2,3,4,5};

If you absolutely must have block operations, then Buffer.BlockCopy can be used to blit data between array locatiosn - for example, you could write the first chunk, then blit it a few times to fill the bulk of the array.