Domanda

I am reading up on c# arrays so my question is initially on arrays.

What does declaring an array actually mean? I know you declare a variable of type array. When I have the following, what is actually happening?

int[] values;

Is it in memory by the time it is declared? If not then where is it? Is the array actually created here?

Then I go and instantiate an the array and initialise it with some values like:

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

Does this actually go and create the array now? I have read that arrays are created when they are declared, others say that arrays are created when they are instantiated. I am trying to get my terminology right.

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 technicallity behind the scenes of arrays.

È stato utile?

Soluzione

What does declaring an array actually mean?

You didn't actually declare an array, you declared an array reference. Big deal in .NET, the difference between reference types and value types is important. Just having the array reference variable isn't enough, an extra step is required to create the array object. Which requires the new keyword. Which physically allocates the storage for the array object in the place where reference type objects are stored, the garbage collected heap.

The same goes for an integer variable

No, big difference. That's a value type. If it isn't a field of a class, not that clear from your question, then it is a local variable of a method. It gets created when the method starts running and poofs out of existence when the method returns. Very highly optimized, the core reason that value types exist in C#. The physical storage location is typically a cpu register or a slot on the stack frame if the method uses too many local variables.

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

Altri suggerimenti

When you declare it like this:

int[] values;

you don't specify the size, so there is no way to know how much memory would be needed for an instatiation. This information is only given in the following line:

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

The memory requirements are deduced from the number of instatiation values (and from the memory requirements of the type int, of course).

When you declare an int like this:

int value;

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

int[] values;

Means that you declare a variable of type int[]. No memory is occupied yet, only a reference is created. The code above is initialized to 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 references the newly created array.

Integers work a little different since they are value-types. Value types are initialized to their default values, in case of integers, the value 0.

If you split the declaration and the initialization, the following happens.

// 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 };

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

Declaring an int will actually create the memory for the integer with default value of 0.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top