+ 3

plzz tell me...how can this do? and what is the out put and why this out put..? I can't understand..

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Person { private static int a=b++; private static int b=++c; private static int c=3; public Person (){ Console.Write(a+":"+b+":"+c); } static void Main(){ Person P = New Person (); } } }

27th Feb 2018, 6:13 PM
Nafis Iqbal
Nafis Iqbal - avatar
1 Antwort
- 1
I'm not sure but I think you have to declare c first, then b and then a. Because the compiler reads from the top to the bottom and you cant use the variable b before it is defined. So if it is c = 3; b = ++c; a = b++; c will be 4, b will be 5 and a will be 4 in the end. The difference between x++ and ++x is that if you assign an other variable the value of x and use the so called post-increment (x++), like you did with a = b++, x, or in your case b, will be incremented after it got used, so that a would actually be b, but then b gets incremented so that b is a+1. If you use the pre-increment (++x), x gets incremented right before using it. So if you write b = ++c, then C gets incremented immediately before asigning the value to be. So if c = 3 and you do b = ++c, then both b and c will be 4 because you incremented c before using it.
28th Feb 2018, 6:25 AM
Kai Schlegel
Kai Schlegel - avatar