c# - Add Current value to a list then compare with the last value -


suppose there series of moving numbers:

10, 20, 30, 50, 24, 26, 28

i add last (current) point 28 in example. list

list<int> mynumber= new list<int>(); mynumber.add(currentnumber); //which 28 in example  

now, list contains 10, 20, 30, 50, 24, 26, 28 // wherein 28 current last value added

then 30 added list

the output should be

current value: 30, last number: 30

mynumber[mynumber.count-1]; 

only returns same output current value: 30 last number in list: 30

the output should current value added list, , last number, before current value added

note: index start in zero

static void main(string[] args) {     int currentnumber = 28;     list<int> mynumber = new list<int> { 10, 20, 30, 50, 24, 26 };     mynumber.add(currentnumber);     mynumber.add(30);      console.writeline("last number: {0}", mynumber[mynumber.count - 1]);     console.writeline("before last number {0}", mynumber[mynumber.count - 2]);     console.read();      // output:     // last number: 30     // before last number: 28 } 

update: think trying do?

static void main(string[] args) {     int currentnumber = 28;     list<int> mynumber = new list<int> { 10, 20, 30, 50, 24, 26 };      mynumber.add(currentnumber);     console.writeline("number: {0}", mynumber[mynumber.count - 1]);      mynumber.add(30);     console.writeline("number: {0}", mynumber[mynumber.count - 1]);     console.read();      // output:     // number: 28     // number: 30 } 

you can mynumber.last() instead of mynumber[mynumber.count - 1].


Comments

Popular posts from this blog

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -