Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Strange IL code from simple C# expression

Strange IL code from simple C# expression

Scheduled Pinned Locked Moved C#
questioncsharpdotnet
29 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrew Kirillov
    wrote on last edited by
    #1

    Hello, guys I found an interesting topic on MS newsgroup. And I'd like to ask you a simple question. What is the value of "i" variable after the code ?

    int i = 5;
    i = i++;
    

    ??? Provide me, please, with an answer you though/expected just after you looked at the code. And then, any ideas why does C# compiler work so ? (if you will run the code, you will understand what I mean). With best regards, Andrew

    A L S L D 5 Replies Last reply
    0
    • A Andrew Kirillov

      Hello, guys I found an interesting topic on MS newsgroup. And I'd like to ask you a simple question. What is the value of "i" variable after the code ?

      int i = 5;
      i = i++;
      

      ??? Provide me, please, with an answer you though/expected just after you looked at the code. And then, any ideas why does C# compiler work so ? (if you will run the code, you will understand what I mean). With best regards, Andrew

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      The value of i will be 5 because the i++ increments the value AFTER the assignment. If you want i to be 6 then you can do it like this: int i = 5; i = ++i; The method above increments the value BEFORE the assignment. By the way, this is not just a C# thing, any C syntax language shoould behave this way (incl. java, javascript, C++ etc..) Si

      A 1 Reply Last reply
      0
      • A Anonymous

        The value of i will be 5 because the i++ increments the value AFTER the assignment. If you want i to be 6 then you can do it like this: int i = 5; i = ++i; The method above increments the value BEFORE the assignment. By the way, this is not just a C# thing, any C syntax language shoould behave this way (incl. java, javascript, C++ etc..) Si

        A Offline
        A Offline
        Andrew Kirillov
        wrote on last edited by
        #3

        Anonymous wrote: By the way, this is not just a C# thing, any C syntax language shoould behave this way (incl. java, javascript, C++ etc..) Yes, maybe other languages also should, but C++ produces 6. (managed and unmanaged versions). With best regards, Andrew

        1 Reply Last reply
        0
        • A Andrew Kirillov

          Hello, guys I found an interesting topic on MS newsgroup. And I'd like to ask you a simple question. What is the value of "i" variable after the code ?

          int i = 5;
          i = i++;
          

          ??? Provide me, please, with an answer you though/expected just after you looked at the code. And then, any ideas why does C# compiler work so ? (if you will run the code, you will understand what I mean). With best regards, Andrew

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I guess "5" It's actually the same with C(++), because the expression i++ is evaluated AFTER the statement.

          A 1 Reply Last reply
          0
          • L Lost User

            I guess "5" It's actually the same with C(++), because the expression i++ is evaluated AFTER the statement.

            A Offline
            A Offline
            Andrew Kirillov
            wrote on last edited by
            #5

            Greeeg wrote: It's actually the same with C(++), Not the same. I've checked on my VS 2003. With best regards, Andrew

            1 Reply Last reply
            0
            • A Andrew Kirillov

              Hello, guys I found an interesting topic on MS newsgroup. And I'd like to ask you a simple question. What is the value of "i" variable after the code ?

              int i = 5;
              i = i++;
              

              ??? Provide me, please, with an answer you though/expected just after you looked at the code. And then, any ideas why does C# compiler work so ? (if you will run the code, you will understand what I mean). With best regards, Andrew

              S Offline
              S Offline
              sreejith ss nair
              wrote on last edited by
              #6

              Problem is there in your example. Try to use different variable to distingush the difference. int i=5,j=0; j=i++; Sreejith Nair [ My Articles ]

              A 1 Reply Last reply
              0
              • S sreejith ss nair

                Problem is there in your example. Try to use different variable to distingush the difference. int i=5,j=0; j=i++; Sreejith Nair [ My Articles ]

                A Offline
                A Offline
                Andrew Kirillov
                wrote on last edited by
                #7

                Sreejith SS Nair wrote: Problem is there in your example. I know that my example is useless. It was interesting another thing: C(++) gives 6, but C# gives 5. Never mind, the C# behavior is good described in MSDN[^]. With best regards, Andrew

                S 1 Reply Last reply
                0
                • A Andrew Kirillov

                  Sreejith SS Nair wrote: Problem is there in your example. I know that my example is useless. It was interesting another thing: C(++) gives 6, but C# gives 5. Never mind, the C# behavior is good described in MSDN[^]. With best regards, Andrew

                  S Offline
                  S Offline
                  sreejith ss nair
                  wrote on last edited by
                  #8

                  You wrote : Provide me, any ideas why does C# compiler work so ? I post answer for your queary. And sorry if you twist the issue. Sreejith Nair [ My Articles ]

                  1 Reply Last reply
                  0
                  • A Andrew Kirillov

                    Hello, guys I found an interesting topic on MS newsgroup. And I'd like to ask you a simple question. What is the value of "i" variable after the code ?

                    int i = 5;
                    i = i++;
                    

                    ??? Provide me, please, with an answer you though/expected just after you looked at the code. And then, any ideas why does C# compiler work so ? (if you will run the code, you will understand what I mean). With best regards, Andrew

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #9

                    IMO this is a compiler bug. From the IL you can see there is some funniness. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

                    1 Reply Last reply
                    0
                    • A Andrew Kirillov

                      Hello, guys I found an interesting topic on MS newsgroup. And I'd like to ask you a simple question. What is the value of "i" variable after the code ?

                      int i = 5;
                      i = i++;
                      

                      ??? Provide me, please, with an answer you though/expected just after you looked at the code. And then, any ideas why does C# compiler work so ? (if you will run the code, you will understand what I mean). With best regards, Andrew

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      Andrew Kirillov wrote: int i = 5; i = i++; Easy, 5. The value of i is incremented after the expression is evaluated. In this example: Andrew Kirillov wrote: int i = 5; i = ++i; Here, i will be 6. The value of i is incremented before the expression is evaluated. This is all defined functionality in the C# language specification. C and C++ do the same thing. 7.5.9 Postfix increment and decrement operators[^] RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                      A 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        Andrew Kirillov wrote: int i = 5; i = i++; Easy, 5. The value of i is incremented after the expression is evaluated. In this example: Andrew Kirillov wrote: int i = 5; i = ++i; Here, i will be 6. The value of i is incremented before the expression is evaluated. This is all defined functionality in the C# language specification. C and C++ do the same thing. 7.5.9 Postfix increment and decrement operators[^] RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                        A Offline
                        A Offline
                        Andrew Kirillov
                        wrote on last edited by
                        #11

                        Dave Kreskowiak wrote: C and C++ do the same thing. No ! I wrote it alredy. I've tested it with VS.NET 2003. C++ (managed and unmanaged) gives 6. With best regards, Andrew

                        W D 2 Replies Last reply
                        0
                        • A Andrew Kirillov

                          Dave Kreskowiak wrote: C and C++ do the same thing. No ! I wrote it alredy. I've tested it with VS.NET 2003. C++ (managed and unmanaged) gives 6. With best regards, Andrew

                          W Offline
                          W Offline
                          Wjousts
                          wrote on last edited by
                          #12

                          Andrew Kirillov wrote: No ! I wrote it alredy. I've tested it with VS.NET 2003. C++ (managed and unmanaged) gives 6. Then I'd say the problem is with C++ not with C#. Try this: int i = 5; if (i++ == 5) { Console::WriteLine("Five"); } else { Console::WriteLine("Six"); } In C# and C++ you should get "Five" as a result.

                          A 1 Reply Last reply
                          0
                          • W Wjousts

                            Andrew Kirillov wrote: No ! I wrote it alredy. I've tested it with VS.NET 2003. C++ (managed and unmanaged) gives 6. Then I'd say the problem is with C++ not with C#. Try this: int i = 5; if (i++ == 5) { Console::WriteLine("Five"); } else { Console::WriteLine("Six"); } In C# and C++ you should get "Five" as a result.

                            A Offline
                            A Offline
                            Andrew Kirillov
                            wrote on last edited by
                            #13

                            Wjousts wrote: Try this: .... In C# and C++ you should get "Five" as a result. Yes, I know. It will be the same in both languages. I was interested in original code I wrote. With best regards, Andrew

                            W 1 Reply Last reply
                            0
                            • A Andrew Kirillov

                              Dave Kreskowiak wrote: C and C++ do the same thing. No ! I wrote it alredy. I've tested it with VS.NET 2003. C++ (managed and unmanaged) gives 6. With best regards, Andrew

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #14

                              So it does... It looks like your right. So what's the point of all this? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                              1 Reply Last reply
                              0
                              • A Andrew Kirillov

                                Wjousts wrote: Try this: .... In C# and C++ you should get "Five" as a result. Yes, I know. It will be the same in both languages. I was interested in original code I wrote. With best regards, Andrew

                                W Offline
                                W Offline
                                Wjousts
                                wrote on last edited by
                                #15

                                Actually you are right, it should be 6 (in your original code) in both C++ and C#. Nasty trick question that. So it would appear to be a problem with the C# compiler.

                                D 1 Reply Last reply
                                0
                                • W Wjousts

                                  Actually you are right, it should be 6 (in your original code) in both C++ and C#. Nasty trick question that. So it would appear to be a problem with the C# compiler.

                                  D Offline
                                  D Offline
                                  Dave Kreskowiak
                                  wrote on last edited by
                                  #16

                                  After doing some digging, the evaluation rules are slightly different between the C++ language specifications and the C# specs. It's not a bug as far the specifications describes the process. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                                  W 1 Reply Last reply
                                  0
                                  • D Dave Kreskowiak

                                    After doing some digging, the evaluation rules are slightly different between the C++ language specifications and the C# specs. It's not a bug as far the specifications describes the process. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                                    W Offline
                                    W Offline
                                    Wjousts
                                    wrote on last edited by
                                    #17

                                    No, I think it is a bug, evaulation rules or not. It's very easy to miss (and I did at first) but the prefix/postfix thing is irrelevant: int i = 5; i = i++; versus int i = 5; i = ++i; Should both end up with i = 6. i++ is the same as i = i + 1 (so is ++i) so really the both code snippets should become int i = 5; i = i; i = i + 1; and int i = 5; i = i + 1; i = i; It shouldn't matter if you do i = i first or i = i + 1 first the result should still be six. As somebody else pointed out, it you do j = i++ then look at the value of j then the prefix/postfix part matters, but because you are putting the result back in to i it should be six either way.

                                    D 1 Reply Last reply
                                    0
                                    • W Wjousts

                                      No, I think it is a bug, evaulation rules or not. It's very easy to miss (and I did at first) but the prefix/postfix thing is irrelevant: int i = 5; i = i++; versus int i = 5; i = ++i; Should both end up with i = 6. i++ is the same as i = i + 1 (so is ++i) so really the both code snippets should become int i = 5; i = i; i = i + 1; and int i = 5; i = i + 1; i = i; It shouldn't matter if you do i = i first or i = i + 1 first the result should still be six. As somebody else pointed out, it you do j = i++ then look at the value of j then the prefix/postfix part matters, but because you are putting the result back in to i it should be six either way.

                                      D Offline
                                      D Offline
                                      Dave Kreskowiak
                                      wrote on last edited by
                                      #18

                                      According the doc's I read in the specifications, the result your seeing is the expected result of the expression. C# in 2002, 2003, and 2005 do the exact same thing. The specifications between versions didn't change. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                                      W 1 Reply Last reply
                                      0
                                      • D Dave Kreskowiak

                                        According the doc's I read in the specifications, the result your seeing is the expected result of the expression. C# in 2002, 2003, and 2005 do the exact same thing. The specifications between versions didn't change. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                                        W Offline
                                        W Offline
                                        Wjousts
                                        wrote on last edited by
                                        #19

                                        The specifications concerning the behavor of postfix and prefix increment aren't relavent. The result should be 6 either way! i = i++; i gets assigned 5 and then i gets incremented. Therefore i = 6.

                                        D D 2 Replies Last reply
                                        0
                                        • W Wjousts

                                          The specifications concerning the behavor of postfix and prefix increment aren't relavent. The result should be 6 either way! i = i++; i gets assigned 5 and then i gets incremented. Therefore i = 6.

                                          D Offline
                                          D Offline
                                          Daniel Grunwald
                                          wrote on last edited by
                                          #20

                                          Wjousts wrote: i gets assigned 5 and then i gets incremented. Therefore i = 6. That's not the normal execution order. The real execution order (in C#) is: Read value of i Increment i Assign old value of i to i Why should the "++" wait until after the assignment? It directly binds to "i": AssignmentExpression: Left=i Op=Assign Right=[UnaryExpression: Op=PostIncrement Expr=i] It behaves differently in C++ because C++ has very strange rules for execution order.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups