1

I had this question on an interview and I was wondering what would you give as answer if you had the same question. Please don't test the program in the IDE.

public class MyClass
{
    private int _myInt;
    public int MyInt
    {
        get { return _myInt; }
        set { _myInt = value; }
    }

    public MyClass()
    {
        MyInt = 1;
    }
}

What should this program print on the screen ? the more important is Why !

var myClass = new MyClass();

myClass.MyInt += myClass.MyInt;
myClass.MyInt = myClass.MyInt +++ 2;

Console.WriteLine(myClass.MyInt);
LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
Wassim AZIRAR
  • 10,823
  • 38
  • 121
  • 174
  • `a +++ b` == `a + (++b)`. – thebjorn Dec 20 '13 at 10:30
  • 2
    I hope the company which was asking this interview question didn't ask it because its codebase contains code like this... – PMF Dec 20 '13 at 10:33
  • News flash, we're not your interviewers. You can put this code in and compile it and see what happens for yourself. Try to reason about what is happening. Run it in a debugger. Whatever. There's no point in _not_ doing this now that you are out of your interview. – Jeff Mercado Dec 20 '13 at 10:35
  • @JeffMercado I'm surprised that you couldn't answer this question ! – Wassim AZIRAR Dec 20 '13 at 10:36
  • Who says he can't? He's given you good advice allowing you to get the answer to the why in the general case. – George Duckett Dec 20 '13 at 10:37
  • No, I can answer it, but I choose not to. This might be fine to ask if you're in conversation with someone away from a computer. But since you have access to one and presumably a compiler, there's no reason why you couldn't try and figure it out yourself. – Jeff Mercado Dec 20 '13 at 10:38
  • possible duplicate of [What's the difference between X = X++; vs X++;?](http://stackoverflow.com/questions/226002/whats-the-difference-between-x-x-vs-x) – nawfal Jul 20 '14 at 08:00

3 Answers3

2

lets look at everything

  1. when you do var myClass = new MyClass();, MyInt = 1, as you have initialized it in the constructor

  2. when you do this myClass.MyInt += myClass.MyInt; MyInt was 1, so you added 1 to itself, so now MyInt is 2

  3. when you do this myClass.MyInt = myClass.MyInt +++ 2;; MyInt was 2 and you added 2 to it so it became 4, post ++ implies, increment after assignment (in this case), so it assigns 4 in MyInt

hence answer should be 4,

but this was a very simple case and this answer almost states that post ++ means evaluate current expression then increment and pre ++ means increment then evaluate current expression, but it is not exactly so(it appears in your case though), read more about it here, by the language designer himself

Community
  • 1
  • 1
Manish Mishra
  • 12,163
  • 5
  • 35
  • 59
1

If you write the third line a litte different it gets a bit clearer:

myClass.MyInt = myClass.MyInt++ + 2;

The post-increment returns 2 and is afterwards incremented. The result is 2 and 2 is added to it. Then the result of the post increment is overwritten again by the result of the addition (2+2)

Flat Eric
  • 7,971
  • 9
  • 36
  • 45
0

Ans would be 4 because ++(post increment operator) has higher precedence then + and + has higher precedence then =

Community
  • 1
  • 1
Anirudha
  • 32,393
  • 7
  • 68
  • 89