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. The Lounge
  3. Strange code example (teaser)

Strange code example (teaser)

Scheduled Pinned Locked Moved The Lounge
helptutorialquestion
7 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.
  • J Offline
    J Offline
    James Lonero
    wrote on last edited by
    #1

    I saw this in a code sample on a JetBrains web page. Can you spot the problem(s)?

    using System;
    public class Class1 {
    private Action _someAction;

    public void Method() {
        var obj1 = new object();
        var obj2 = new object();
    
        \_someAction += () => {
            Console.WriteLine(obj1);
            Console.WriteLine(obj2);
        };
    
        // "Implicitly captured closure: obj2"
        \_someAction += () => {
            Console.WriteLine(obj1);
        };
    }
    

    }

    This should be easy to figure out.

    J L OriginalGriffO P 4 Replies Last reply
    0
    • J James Lonero

      I saw this in a code sample on a JetBrains web page. Can you spot the problem(s)?

      using System;
      public class Class1 {
      private Action _someAction;

      public void Method() {
          var obj1 = new object();
          var obj2 = new object();
      
          \_someAction += () => {
              Console.WriteLine(obj1);
              Console.WriteLine(obj2);
          };
      
          // "Implicitly captured closure: obj2"
          \_someAction += () => {
              Console.WriteLine(obj1);
          };
      }
      

      }

      This should be easy to figure out.

      J Offline
      J Offline
      Jon McKee
      wrote on last edited by
      #2

      Am I missing something? Besides the erroneous comment (obj1 is captured, not obj2) I see no issues :doh:

      N 1 Reply Last reply
      0
      • J Jon McKee

        Am I missing something? Besides the erroneous comment (obj1 is captured, not obj2) I see no issues :doh:

        N Offline
        N Offline
        Nish Nishant
        wrote on last edited by
        #3

        Not sure what that code's trying to do, but the delegate gets both functions added to its invocation list, and this is repeated each time the containing method is called.

        Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

        J 1 Reply Last reply
        0
        • N Nish Nishant

          Not sure what that code's trying to do, but the delegate gets both functions added to its invocation list, and this is repeated each time the containing method is called.

          Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

          J Offline
          J Offline
          Jon McKee
          wrote on last edited by
          #4

          Yea. I guess I was assuming there was another error - not just pointless code. With the naming, I can't even be absolutely sure that isn't intended behavior :laugh:

          1 Reply Last reply
          0
          • J James Lonero

            I saw this in a code sample on a JetBrains web page. Can you spot the problem(s)?

            using System;
            public class Class1 {
            private Action _someAction;

            public void Method() {
                var obj1 = new object();
                var obj2 = new object();
            
                \_someAction += () => {
                    Console.WriteLine(obj1);
                    Console.WriteLine(obj2);
                };
            
                // "Implicitly captured closure: obj2"
                \_someAction += () => {
                    Console.WriteLine(obj1);
                };
            }
            

            }

            This should be easy to figure out.

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

            This is why you don't give matches or scissors to unsupervised children under 10 years old. If only software devs would consider similar wisdom for programmers with under 10 years experience.

            Signature ready for installation. Please Reboot now.

            1 Reply Last reply
            0
            • J James Lonero

              I saw this in a code sample on a JetBrains web page. Can you spot the problem(s)?

              using System;
              public class Class1 {
              private Action _someAction;

              public void Method() {
                  var obj1 = new object();
                  var obj2 = new object();
              
                  \_someAction += () => {
                      Console.WriteLine(obj1);
                      Console.WriteLine(obj2);
                  };
              
                  // "Implicitly captured closure: obj2"
                  \_someAction += () => {
                      Console.WriteLine(obj1);
                  };
              }
              

              }

              This should be easy to figure out.

              OriginalGriffO Online
              OriginalGriffO Online
              OriginalGriff
              wrote on last edited by
              #6

              James Lonero wrote:

              Can you spot the problem(s)?

              Yes. 1) Somebody actually wrote it. 2) See (1).

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • J James Lonero

                I saw this in a code sample on a JetBrains web page. Can you spot the problem(s)?

                using System;
                public class Class1 {
                private Action _someAction;

                public void Method() {
                    var obj1 = new object();
                    var obj2 = new object();
                
                    \_someAction += () => {
                        Console.WriteLine(obj1);
                        Console.WriteLine(obj2);
                    };
                
                    // "Implicitly captured closure: obj2"
                    \_someAction += () => {
                        Console.WriteLine(obj1);
                    };
                }
                

                }

                This should be easy to figure out.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                And what invokes the action?

                This space for rent

                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