What does declaring and instantiating a C # array mean?

I read on C # arrays, so my question is initially on arrays.

What does an array declaration mean? I know that you are declaring a variable of type array. When I have the following, what actually happens?

int[] values; 

Is he in memory at the time of his announcement? If not, where is it? Is the array actually created here?

Then I go and instantiate the array and initialize it with some values, for example:

 int[] values = new int[] { 1, 2, 3 }; 

Is this really happening and now an array is being created? I read that arrays are created when they are declared, others say that arrays are created when they are created. I am trying to get the right terminology.

The same goes for an integer variable. If I have:

 int value; 

and

 int value = 1; 

When is int created? When is it added to memory?

Sorry for the dumb questions. I understand the concept, but would like to know the technique behind the scenes of arrays.

+6
source share
4 answers

What does an array declaration mean?

You did not actually declare an array, you provided a reference to the array. The big deal in .NET is that the distinction between reference types and value types is important. Just having a reference array variable is not enough, an additional step is required to create an array object. This requires a new keyword. What physically allocates storage for the array object in the place where the objects of the reference type are stored, garbage collected in a heap.

The same goes for an integer variable

No, a big difference. This is a value type. If this is not a class field, it is not so clear from your question, then this is a local method variable. It is created when the method starts to work, and poofs exit it when the method returns. The main reason why value types exist in C # is very optimized. Physical storage is usually a processor register or slot in the stack stack if the method uses too many local variables.

If it is actually a member of the class, it is created when the class object is created. Just like an array, on a GC heap with a new keyword.

+6
source
 int[] values; 

means that you are declaring a variable of type int []. While the memory is not yet occupied, only a link is created. The above code is initialized with a null reference.

 int[] values = new int[] { 1, 2, 3 }; 

This code declares a variable of type int [] and immediately creates an array. The variable refers to the newly created array.

Integers are slightly different from each other because they are value types. Value types are initialized to default values; in the case of integers, the value is 0.

If you separate the declaration and the initialization, the following will happen.

 // This declares a variable int[] values; // This creates the array, and initializes the variable with the newly created array. values = new int[] { 1, 2, 3 }; 
+1
source

When you declare an array, internally everything that is created is a null pointer that is of type int[] . When you use the new keyword, as in your example, or use new int[6] , at this time the system allocates memory for the size of the array.

An int declaration will actually create memory for an integer with a default value of 0.

+1
source

When you declare it as follows:

 int[] values; 

you do not specify a size, so there is no way to find out how much memory is needed to ensure stability. This information is provided only on the following line:

 values = new int[] { 1, 2, 3 }; 

Memory requirements are derived from the number of settings (and from memory requirements of type int , of course).

When you declare an int as follows:

 int value; 

memory requirements are known and cannot change (since int is a value type). This variable can (and will) be created immediately. If you do not specify an initial value, it will have a default value, which for int is 0 .

+1
source

Source: https://habr.com/ru/post/949218/


All Articles