Cannot restore the contents of an array or collection position via foreach. You have to use the for classic for this.
The Array, in a simplistic way, it is stored in memory as follows:
Each of the items(values) assigned to the Array are stored in memory positions.
Endereço Valor
#00010 "Portugues"
#00011 "matemática"
#00013 "história",
#00014 "física"
At each position of Array a memory position is assigned that holds, not the value itself, but the address where the value was stored.
Endereço Valor indice
#00001 #00010 [0]
#00002 #00011 [1]
#00003 #00012 [2]
#00004 #00013 [3]
What the variable of the foreach curso
received is the reference(address) where the value corresponding to the relevant index is stored and not the address of the Array
When the condition curso.equals(cursoModificar)
is true, the variable curso
has as value(reference) the address #00010.
The variable cursoNovo
also references a memory position, the one assigned to it when the method was called, e.g..: #00030.
When the line is executed curso = cursoNovo;
, curso
refers to the address #00030, the one where the string "sciences" is, however nothing was changed in the Array. The position [0]
, which is stored at the address #00001, continue to reference the address #00010, that continues to guard the String "Portugues".
Confused? Maybe not if you can think of addresses instead of values.
Note: Address values are indicative only.
You changed the
==
forEquals()
? Did it work? As I use more C#, I usually forget that detail. Confirm this and I reply.– Maniero
I switched yes, but no funnel yet...
– Aline