c# - Assignment of value to specified index in array has no effect -


i have created minimal example reproducing problem (or rather misunderstanding):

string text = @"eaisjdoaisjdoaisjdai_osjdaisodjasizzi_ojiozaziasjz_"; int[] score = new int[123];  foreach(char letter in text) {    int val = score[letter]; //give me value stored @ index    score[letter] = val++; //increment , store array @ index }  ... 

debugging through above, val correctly being assigned value @ specified index of array. when incremented, val not assigned array. why that?

the picture shows immediate window evaluating value of val when retrieving array, value of score[letter] after being assigned , incremented value of val

i'm doing stupid can't quite figure out what.

enter image description here

this because using post-increment operator, increments value after returning it.
change pre-increment operator ++val , should work.

from ++ operator documentation:

the first form (++val) prefix increment operation. result of operation value of operand after has been incremented.

the second form (val++) postfix increment operation. result of operation value of operand before has been incremented.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -