What is a equivalent of Delphi FillChar in C#?

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


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.

I'm assuming you want to fill a byte array with zeros (as that's what FillChar is mostly used for in Delphi).

.NET is guaranteed to initialize all the values in a byte array to zero on creation, so generally FillChar in .NET isn't necessary.

So saying:

byte[] buffer = new byte[1024];

will create a buffer of 1024 zero bytes.

If you need to zero the bytes after the buffer has been used, you could consider just discarding your byte array and declaring a new one (that's if you don't mind having the GC work a bit harder cleaning up after you).

Need Your Help

Windows Mobile (C#) - Communicating between phone and PC

c# windows-mobile

I'm working on a project where a program running on the mobile phone needs to communicate with a program running on the PC it's connected to. Ideally, I'd like to use USB, WiFi, whatever to commun...

Using flows in webfragments

cdi jsf-2.2 flow-scope

I'm trying to build a webapp that includes a webfragment which contains a flow.