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. Call Dispose on anything

Call Dispose on anything

Scheduled Pinned Locked Moved Clever Code
question
44 Posts 11 Posters 8 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.
  • P PIEBALDconsult

    Luc Pattyn wrote:

    I would not want to write string s=new string(new char[]{'H','e','l','l','o'}; !

    I don't see why you'd have to.

    Luc Pattyn wrote:

    foreach

    Oh, yeah, that was the other one, but it should work too.

    S Offline
    S Offline
    S Senthil Kumar
    wrote on last edited by
    #35

    How about lock then?

    Regards Senthil _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

    P 1 Reply Last reply
    0
    • S supercat9

      I just looked at the auto-generated code, and it seems that there is a #Region created with a parameterless Dispose() which calls Dispose(True) and SuppressFinalize, but does not actually contain a finalizer (though if a finalizer is created, it's supposed to call Dispose(False)). That seems like a good approach to doing things. Your comment about minimizing the actual Finalizable "footprint" is a good one. Among other things, if an object controls multiple unmanaged objects, splitting things up would allow separate finalizers for each. BTW, are there any sorts of well-designed objects which use finalizers but do not implement iDisposable? I still find it strange that all objects inherit Finalize, but not Dispose.

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #36

      supercat9 wrote:

      I still find it strange that all objects inherit Finalize, but not Dispose.

      I think it's because the CLR calls Finalize, but developers are supposed to call Dispose. The CLR doesn't consider an object to be finalizable if it doesn't override the implemenation derived from Object. If all types have Dispose, it'd be hard for developers to figure out the types for which they'll have to call Dispose.

      supercat9 wrote:

      BTW, are there any sorts of well-designed objects which use finalizers but do not implement iDisposable?

      That'd be a bug, wouldn't it? The object holds resources that need to be released, but it doesn't allow the user to release them early.

      Regards Senthil _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

      1 Reply Last reply
      0
      • S S Senthil Kumar

        How about lock then?

        Regards Senthil _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

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

        I don't think that relies specifically on anything in .net; it could be implemented differently.

        S 1 Reply Last reply
        0
        • P PIEBALDconsult

          I don't think that relies specifically on anything in .net; it could be implemented differently.

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #38

          Well, it calls Monitor.Enter and Monitor.Exit, and they are specific types in the .NET BCL.

          Regards Senthil _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

          P 1 Reply Last reply
          0
          • S S Senthil Kumar

            Well, it calls Monitor.Enter and Monitor.Exit, and they are specific types in the .NET BCL.

            Regards Senthil _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

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

            That's just implementation; a different implementation could do something different.

            S 1 Reply Last reply
            0
            • P PIEBALDconsult

              That's just implementation; a different implementation could do something different.

              S Offline
              S Offline
              S Senthil Kumar
              wrote on last edited by
              #40

              Can't you make the same argument for using as well? Your implementation could ignore types that don't implement IDisposable, for example.

              Regards Senthil _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

              P 1 Reply Last reply
              0
              • S S Senthil Kumar

                Can't you make the same argument for using as well? Your implementation could ignore types that don't implement IDisposable, for example.

                Regards Senthil _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

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

                Yes, I suppose Duck Typing would do that, maybe for foreach as well.

                1 Reply Last reply
                0
                • S supercat9

                  I recognize that extension methods make Intellisense more useful, but a similar result could have been achieved by allowing methods to be tagged as auto-Intellisense, so that they would be listed in the Intellisense pop-up. Typing "textbox1.Text." might include a reverse function in the pop-up list, but selecting it would replace the period after "text" with a right-paren, and insert "StrUtil.Reverse(" before it (if the method took more parameters, it would use a comma rather than a right-paren. Actually, one could leverage Intellisense further by using a double-dot notation for extension methods (since double-dot is not otherwise legitimate, Intellisense would know that when a programmer typed "objectname.." he wanted a list of declared applicable methods. For that matter, that might not have been a bad syntax for extension methods. Conceptually, I wouldn't mind extension methods so much if they were distinct from real ones. Having code change meaning on a rebuild, though, seems like a disaster waiting to happen, unless one always performs full rebuilds whenever anything changes.

                  D Offline
                  D Offline
                  dojohansen
                  wrote on last edited by
                  #42

                  I agree with most of this. I like the idea of having the IDE discover utility methods based on some declarative syntax to mark a method as such - and the Method(this type obj) syntax is fine by me for this purpose. If the IDE modified the code so it instead read Class.Method(obj), using normal static method syntax, it would have been better IMO. However, I don't quite understand your most serious criticism. How could extention methods cause code to change simply because you rebuild? The only code change I can think of that could possibly modify anything would be if a namespace had been replaced by another and this caused some name to resolve to something different from what it did before. But that is true of any members, not just extension methods. I suppose it is slightly more likely to happen with extension methods though, since only the method name and parameter list needs to match in the two namespaces, whereas normal static method syntax would have required the class name to also be the same for the two implementations in order to produce the same effect.

                  S 1 Reply Last reply
                  0
                  • D dojohansen

                    I agree with most of this. I like the idea of having the IDE discover utility methods based on some declarative syntax to mark a method as such - and the Method(this type obj) syntax is fine by me for this purpose. If the IDE modified the code so it instead read Class.Method(obj), using normal static method syntax, it would have been better IMO. However, I don't quite understand your most serious criticism. How could extention methods cause code to change simply because you rebuild? The only code change I can think of that could possibly modify anything would be if a namespace had been replaced by another and this caused some name to resolve to something different from what it did before. But that is true of any members, not just extension methods. I suppose it is slightly more likely to happen with extension methods though, since only the method name and parameter list needs to match in the two namespaces, whereas normal static method syntax would have required the class name to also be the same for the two implementations in order to produce the same effect.

                    S Offline
                    S Offline
                    supercat9
                    wrote on last edited by
                    #43

                    dojohansen wrote:

                    The only code change I can think of that could possibly modify anything would be if a namespace had been replaced by another and this caused some name to resolve to something different from what it did before.

                    Scenario: the version of class "foo" with which a program was written does not have a method named "bar", but an extension method of that name exists and is used within class "boz". Later on, a method named "bar" is added to class "foo". Unless it is rebuilt, class "boz" will use the extension method, but if it is rebuilt it will use the bar() method from class foo. The scenario could not exist without extension methods, since code which tried to use method bar() on an object of class foo would never be able to compile unless or until class foo actually implemented such a method. Unless a functional program used reflection, or else a combination of late binding and error trapping, the addition of a method to a class could not change the program's semantic behavior.

                    D 1 Reply Last reply
                    0
                    • S supercat9

                      dojohansen wrote:

                      The only code change I can think of that could possibly modify anything would be if a namespace had been replaced by another and this caused some name to resolve to something different from what it did before.

                      Scenario: the version of class "foo" with which a program was written does not have a method named "bar", but an extension method of that name exists and is used within class "boz". Later on, a method named "bar" is added to class "foo". Unless it is rebuilt, class "boz" will use the extension method, but if it is rebuilt it will use the bar() method from class foo. The scenario could not exist without extension methods, since code which tried to use method bar() on an object of class foo would never be able to compile unless or until class foo actually implemented such a method. Unless a functional program used reflection, or else a combination of late binding and error trapping, the addition of a method to a class could not change the program's semantic behavior.

                      D Offline
                      D Offline
                      dojohansen
                      wrote on last edited by
                      #44

                      I don't think your scenario description quite worked out as you intended. After a little reflection I suppose you forgot to mention that boz extends foo..?

                      supercat9 wrote:

                      the version of class "foo" with which a program was written does not have a method named "bar", but an extension method of that name exists and is used within class "boz".

                      Translation:

                      class foo {}

                      class boz : foo {
                      public void method() { this.bar(); }
                      }

                      We neither know nor care where the extension method, bar, is declared - but it exists and so this builds.

                      supercat9 wrote:

                      Later on, a method named "bar" is added to class "foo". Unless it is rebuilt, class "boz" will use the extension method, but if it is rebuilt it will use the bar() method from class foo.

                      class foo {
                      protected void bar() { drink(6); Thread.Sleep(TimeSpan.FromHours(11)); } //EDIT: Increased sleep interval :D
                      }

                      class boz : foo {
                      public void method() { this.bar(); }
                      }

                      Indeed it does - if and only if boz extends foo. Also, in this particular example, a more appropriate class name might have been booze. ;)

                      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