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. Other Discussions
  3. Clever Code
  4. C# Compiler bug or Subtle bug, I don't know, but this is certainly a doozy!

C# Compiler bug or Subtle bug, I don't know, but this is certainly a doozy!

Scheduled Pinned Locked Moved Clever Code
helpcsharplinqfunctionalannouncement
9 Posts 6 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.
  • S Offline
    S Offline
    Super Lloyd
    wrote on last edited by
    #1

    I have a problem with Lambda expression, looks like a C# compiler bug to me.... This piece of code

    static void Main(string[] args)
    {
    var names = new List<string> { "Lloyd", "Stuart", "Gabe", "Mark" };
    var actions = new List<Action>();
    foreach (var item in names)
    actions.Add(() => Console.WriteLine(item));
    foreach (var a in actions)
    a();
    }

    Will print "Mark, Mark, Mark, Mark" Instead of "Lloyd, Stuart, Gabe, Mark" as it should.... I can fix it with the slightly modified version below..

    static void Main(string[] args)
    {
    var names = new List<string> { "Lloyd", "Stuart", "Gabe", "Mark32" };
    var actions = new List<Action>();
    foreach (var item in names)
    {
    string s = item;
    actions.Add(() => Console.WriteLine(s));
    }
    foreach (var a in actions)
    a();
    }

    But I think it's a C# compiler bug...

    A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

    P S L 3 Replies Last reply
    0
    • S Super Lloyd

      I have a problem with Lambda expression, looks like a C# compiler bug to me.... This piece of code

      static void Main(string[] args)
      {
      var names = new List<string> { "Lloyd", "Stuart", "Gabe", "Mark" };
      var actions = new List<Action>();
      foreach (var item in names)
      actions.Add(() => Console.WriteLine(item));
      foreach (var a in actions)
      a();
      }

      Will print "Mark, Mark, Mark, Mark" Instead of "Lloyd, Stuart, Gabe, Mark" as it should.... I can fix it with the slightly modified version below..

      static void Main(string[] args)
      {
      var names = new List<string> { "Lloyd", "Stuart", "Gabe", "Mark32" };
      var actions = new List<Action>();
      foreach (var item in names)
      {
      string s = item;
      actions.Add(() => Console.WriteLine(s));
      }
      foreach (var a in actions)
      a();
      }

      But I think it's a C# compiler bug...

      A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Yeah, it should print, "Chuck Norris,Chuck Norris,Chuck Norris,Chuck Norris". :-D

      1 Reply Last reply
      0
      • S Super Lloyd

        I have a problem with Lambda expression, looks like a C# compiler bug to me.... This piece of code

        static void Main(string[] args)
        {
        var names = new List<string> { "Lloyd", "Stuart", "Gabe", "Mark" };
        var actions = new List<Action>();
        foreach (var item in names)
        actions.Add(() => Console.WriteLine(item));
        foreach (var a in actions)
        a();
        }

        Will print "Mark, Mark, Mark, Mark" Instead of "Lloyd, Stuart, Gabe, Mark" as it should.... I can fix it with the slightly modified version below..

        static void Main(string[] args)
        {
        var names = new List<string> { "Lloyd", "Stuart", "Gabe", "Mark32" };
        var actions = new List<Action>();
        foreach (var item in names)
        {
        string s = item;
        actions.Add(() => Console.WriteLine(s));
        }
        foreach (var a in actions)
        a();
        }

        But I think it's a C# compiler bug...

        A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

        S Offline
        S Offline
        Steve Hansen
        wrote on last edited by
        #3

        ReSharper gives a warning about that. Let's just keep it at a feature.

        1 Reply Last reply
        0
        • S Super Lloyd

          I have a problem with Lambda expression, looks like a C# compiler bug to me.... This piece of code

          static void Main(string[] args)
          {
          var names = new List<string> { "Lloyd", "Stuart", "Gabe", "Mark" };
          var actions = new List<Action>();
          foreach (var item in names)
          actions.Add(() => Console.WriteLine(item));
          foreach (var a in actions)
          a();
          }

          Will print "Mark, Mark, Mark, Mark" Instead of "Lloyd, Stuart, Gabe, Mark" as it should.... I can fix it with the slightly modified version below..

          static void Main(string[] args)
          {
          var names = new List<string> { "Lloyd", "Stuart", "Gabe", "Mark32" };
          var actions = new List<Action>();
          foreach (var item in names)
          {
          string s = item;
          actions.Add(() => Console.WriteLine(s));
          }
          foreach (var a in actions)
          a();
          }

          But I think it's a C# compiler bug...

          A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

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

          The bug is in your understanding of how the C# compiler captures free variables.

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 beta 1 - out now!
          ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

          S D A 3 Replies Last reply
          0
          • L leppie

            The bug is in your understanding of how the C# compiler captures free variables.

            xacc.ide - now with TabsToSpaces support
            IronScheme - 1.0 beta 1 - out now!
            ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

            S Offline
            S Offline
            Super Lloyd
            wrote on last edited by
            #5

            I figured that out, but I think it's subtle!.. Although, truth to tell, I didn't gave much though to that, I just thought "ho cool, like in Java" and went on...

            A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

            1 Reply Last reply
            0
            • L leppie

              The bug is in your understanding of how the C# compiler captures free variables.

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 beta 1 - out now!
              ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

              D Offline
              D Offline
              Dan Neely
              wrote on last edited by
              #6

              SO what is the compiler doing behind the scenes?

              Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

              L 1 Reply Last reply
              0
              • D Dan Neely

                SO what is the compiler doing behind the scenes?

                Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

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

                dan neely wrote:

                SO what is the compiler doing behind the scenes?

                It hoists the local variables used in a 'subscope' from the current scope into a 'closure' class. All references to hoisted variables even outside the 'subscope' is referenced via the closure.

                xacc.ide - now with TabsToSpaces support
                IronScheme - 1.0 beta 1 - out now!
                ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

                1 Reply Last reply
                0
                • L leppie

                  The bug is in your understanding of how the C# compiler captures free variables.

                  xacc.ide - now with TabsToSpaces support
                  IronScheme - 1.0 beta 1 - out now!
                  ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

                  A Offline
                  A Offline
                  antoncl
                  wrote on last edited by
                  #8

                  Bugs can be a lot of things, even language design and semantics can be riddled with "bugs". This is just one example of how unintuitive things can get when lambda functions are implemented the "wrong" way. The problem is that the runtime doesn't capture the dynamic environment when the lambda function "Console.WriteLine(...)" is created, which would be what I believe is the intuitive thing to do. If lambda functions had been implemented "the right way", the dynamic environment as it existed when the lambda function was defined would have been preserved.

                  L 1 Reply Last reply
                  0
                  • A antoncl

                    Bugs can be a lot of things, even language design and semantics can be riddled with "bugs". This is just one example of how unintuitive things can get when lambda functions are implemented the "wrong" way. The problem is that the runtime doesn't capture the dynamic environment when the lambda function "Console.WriteLine(...)" is created, which would be what I believe is the intuitive thing to do. If lambda functions had been implemented "the right way", the dynamic environment as it existed when the lambda function was defined would have been preserved.

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

                    antoncl wrote:

                    If lambda functions had been implemented "the right way", the dynamic environment as it existed when the lambda function was defined would have been preserved.

                    It is being preserved, but you, the user, is modifying the value of the captured variable outside the closure. And by design the closure should see the side-effect. This is exactly the way any language with lexically scoped closures would behave, like Scheme (and probably python and ruby and F#), but NOT Common Lisp.

                    xacc.ide - now with TabsToSpaces support
                    IronScheme - 1.0 beta 1 - out now!
                    ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

                    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