# Simple problem with list of integer RSS

## 8 replies

Last post Aug 24, 2017 05:25 AM by PaulTheSmith

Member

1 Points

22 Posts

### Simple problem with list of integer

Hi,

Does anyone know what is the point with this simple code:

```var tab = new List<int[]>();
var nxt = new int[3];

for (int i = 0; i < 3; i++)
{
nxt[1] = i + 1;
}```

I want to produce this result:

[0, 0, 0]

[0, 1, 0]

[0, 2, 0]

...

But result is:

[0, 3, 0]

[0, 3, 0]

[0, 3, 0]

Thx

All-Star

49391 Points

9519 Posts

### Re: Simple problem with list of integer

#### int32

var nxt = new int[3];

You have created nxt variable outside of loop so the result will get overwritten on each for loop execution and finally you will see only final output which is 0 3 0.

Try with the below implemenation

``` var tab = new List<int[]>();
for (int i = 0; i < 3; i++)
{
var nxt = new int[3];
nxt[1] = i + 1;
}```

Thanks,
A2H
My Blog | Dotnet Funda

Member

1 Points

22 Posts

### Re: Simple problem with list of integer

Ok,

I understand that, but why variable x is changed after nxt changing?

```var tab = new List<int[]>();
var nxt = new int[3];

for (int i = 0; i < 3; i++)
{
var x = nxt;
nxt[1] = i + 1;     Console.WriteLine(x[0] + ", " + x[1] + ", " + x[2]); //There x has new value of nxt
}```

After first iteration, when compiler is not yet on line when x is assigned

`nxt[1] = i + 1;`

x has new value, why?

Thx

Contributor

5520 Points

2293 Posts

### Re: Simple problem with list of integer

Aug 23, 2017 07:30 AM|Eric Du|LINK

Hi int32,

According to your description and code, as far as I know, because you have set the nxt as the global variable, so when you execute the next loop, then variable value is previous value, it is not the new initial value. I hope you could understand.

Best Regards,

Eric Du

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Member

1 Points

22 Posts

### Re: Simple problem with list of integer

Ok but:

``` var x = nxt;
nxt[1] = i + 1; Console.WriteLine(x[0] + ", " + x[1] + ", " + x[2]); //Here x should have original value of nxt, not nxt[1] = i+1```

Try it and you will see that it's confusing.

Thx

Contributor

5520 Points

2293 Posts

### Re: Simple problem with list of integer

Aug 23, 2017 09:59 AM|Eric Du|LINK

Hi int32,

As far as I know, the array is reference type, variable X and nxt share the same memory address, exist value[0,0,0] in this memory address in the memory, so if you change the value of  this memory address, the variable X and nxt value is the same.  You need check the reference type:

C# Concepts: Value vs Reference Types:

Best Regards,

Eric Du

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Member

700 Points

336 Posts

### Re: Simple problem with list of integer

#### int32

but why variable x is changed after nxt changing
There are three 'things' in this code.

Firstly, the 'new int[3]' expression allocates some memory to hold some integers.

Then the 'nxt' reference variable is created.  That variable points to, references, the memory that was just allocated.

A little later you create an 'x' reference variable.  Thar variable is set equal to the 'nxt' variable.  But, remember, the 'nxt' variable is a pointer or reference to some memory.  So, now the 'x' variable is also a pointer to that memory.

The statement 'nxt[1] = something' changes the second integer in the memory that 'nxt' references.  The 'x' variable still references that memory which you just changed.

It is quite common to say 'element one of the nxt array has been changed' but it would be more precise to say ' element one of the array referenced by the nxt variable has been changed'

Since 'x' and 'nxt' reference the same object (the array that was created) the object (array) can be changed by using either reference.

(You might also find it useful to bear in mind that you do not change 'nxt' or 'x', they always point to the same piece of memory.)

Member

1 Points

22 Posts

### Re: Simple problem with list of integer

Ok,

I understand memory reference, but how to resolve problem? I tried this short code in python and unfortunately it works well.

Member

700 Points

336 Posts