Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.Initialize - Why does this method exist?

Tags:

arrays

c#

.net

I stumbled upon a method today. I'm talking about: Array.Initialize().

According to the documentation:

This method is designed to help compilers support value-type arrays; most users do not need this method.

How does this method is responsible for making the compiler support value types? As far as I'm concerned this method just:

Initializes every element of the value-type Array by calling the default constructor of the value type.

Also, why is it public? I don't see myself with the need of calling this method, compilers already initialize arrays when created, so manually calling this method will be redundant and useless.

Even when my intention would be resetting the values of an array, I would still not call it, I would create a new one. array = new int[].

So, it seems that this method exist just for the sake of the compiler. Why is this? Can anyone give me some more details?

like image 375
Matias Cicero Avatar asked Nov 25 '15 14:11

Matias Cicero


People also ask

Do you have to initialize array C#?

Assigning Values to an ArrayWhen you create an array, C# compiler implicitly initializes each array element to a default value depending on the array type. For example, for an int array all elements are initialized to 0.

What is array initialization in C#?

Array InitializationYou can initialize the elements of an array when you declare the array. The length specifier isn't needed because it's inferred by the number of elements in the initialization list. For example: C# Copy. int[] array1 = new int[] { 1, 3, 5, 7, 9 };

How do you initialize an array of objects in C#?

Initialize Arrays in C# with Known Number of Elements We can initialize an array with its type and size: var students = new string[2]; var students = new string[2]; Here, we initialize and specify the size of the string array.

Should you initialize an array variable to 0?

Historically we would initialize an array to 0 to prevent bad data (remnants of other memory usage) from being in the array. Show activity on this post. You need to initialize it for the same reasons you would initialize any other variable, whether it's an array or not.


2 Answers

It's worth noting that the rules of .NET are different to the rules of C#.

There are things we can do in .NET that we can't do in C#, generally either because the code is not verifiable (ref return types for example) or because they could introduce some confusion.

In C# structs cannot have a defined parameterless constructor, and calling new SomeValueType() works by creating a zero-filled portion of memory (all fields therefore being 0 for numeric types, null for reference types, and the result of this same rule again for other value-types).

In .NET you can have a parameterless constructor on a value type.

It's probably a bad idea to do so. For one thing the rules about just when it is called and just when the memory of the value is zero-filled, and what happens upon assignment in different cases aren't entirely simple (e.g. new SomeValueType() will call it but new T() in a generic method where T is SomeValueType will not!). Life is simpler if the result of new SomeValueType() will always be zero-filling. That no doubt influenced the design of C# not allowing this even though .NET does.

For this reason, Array.Initialize() will never make sense on new arrays of any type that was written in C#, because calling the constructor and zero-filling is the same thing.

But by the same token, it's possible for a type to be written in another .NET language (at the very least, you can do it in CIL) that does have a parameterless constructor that actually has an effect. And for that reason its possible that a compiler for such a language would want its equivalent to new SomeValueType[3] to call that constructor on all the types in the array. And therefore it's sensible to have a method in the framework that allows such a fill to be done, so that a compiler for such a language can make use of it.

Also, why is it public?

So it can be called by code produced by such a hypothetical constructor even in a context where security restrictions prevent it from calling private methods of another assembly.

like image 66
Jon Hanna Avatar answered Oct 29 '22 22:10

Jon Hanna


For me myself it looks like the Initialize() method runs through the array and recreates the Value Types within. So with a new array you get a new empty array and so you get with Array.Clear(), but with Array.Initialize() you get an Array full of fresh created Value Types (types and length based on the old array).

And that should be all of the difference.

like image 23
David Avatar answered Oct 29 '22 22:10

David