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 141 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 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