What is a equivalent of Delphi FillChar in C#?

This is my first question so I would like to say hello to everyone.

My friend was programming in Delphi. Now he is learning C# and asked me, if I know C# eqivalent of Delphi's FillChar.

Could you please help me to help him?

Kind regards


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

Errors with basic PHP template using eval()

php templates eval task-parallel-library

I'm just about ready to cry. I have read the php.net manual page, tried a dozen Google searches with various phrases, and scanned hundreds of stackoverflow threads. I have seen this method in use, ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.