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. using statement equivalent

using statement equivalent

Scheduled Pinned Locked Moved C#
question
17 Posts 5 Posters 1 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.
  • D dan sh

    Why and when would you write a code like that? As far as its equivalent is concerned, just see the IL through ILDAsm or reflector and it would make things a bit clear. :)

    modified on Monday, June 14, 2010 11:55 AM

    A Offline
    A Offline
    Al Beback
    wrote on last edited by
    #8

    d@nish wrote:

    Why and when would you write a code like that?

    When you're interested in keeping track of when "SomeCode" begins and ends -- for logging, profiling, etc.

    ShamWow

    D 1 Reply Last reply
    0
    • A Al Beback

      d@nish wrote:

      Why and when would you write a code like that?

      When you're interested in keeping track of when "SomeCode" begins and ends -- for logging, profiling, etc.

      ShamWow

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #9

      Use interceptor pattern for that. Check out Enterprise Library's Policy Injection Block to see how logging can be done before and after method call. (Search LogCallHandler)

      1 Reply Last reply
      0
      • A Al Beback

        I've been looking around for an answer to this puzzling question, but so far no luck: What's the compiler's try/finally equivalent of this code:

        using (new SomeIDisposableClass())
        {
        SomeCode();
        }

        Thanks!

        ShamWow

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

        Wow, everyone got it wrong :) Debug or Release? The debug code (bugs in Reflector corrected by looking at the MSIL)

        SomeIDisposableClass CS$3$0000;
        bool CS$4$0001;
        CS$3$0000 = new SomeIDisposableClass();
        

        Label_0007:
        try
        {
        SomeCode();
        goto Label_0021;
        }
        finally
        {
        Label_0011:
        CS$4$0001 = CS$3$0000 == null;
        if (CS$4$0001)
        {
        goto Label_0020;
        }
        CS$3$0000.Dispose();
        Label_0020:;
        }
        Label_0021:;

        And yes, it is silly. It's storing the result of the null-check The Release code

        SomeIDisposableClass CS$3$0000;
        CS$3$0000 = new SomeIDisposableClass();
        

        Label_0006:
        try
        {
        SomeCode();
        goto Label_0017;
        }
        finally
        {
        Label_000D:
        if (CS$3$0000 == null)
        {
        goto Label_0016;
        }
        CS$3$0000.Dispose();
        Label_0016:;
        }
        Label_0017:

        edit: the rules for goto-within-try are If the goto statement exits one or more try blocks with associated finally blocks, control is initially transferred to the finally block of the innermost try statement. When and if control reaches the end point of a finally block, control is transferred to the finally block of the next enclosing try statement. This process is repeated until the finally blocks of all intervening try statements have been executed.

        L A 2 Replies Last reply
        0
        • D dan sh

          Why and when would you write a code like that? As far as its equivalent is concerned, just see the IL through ILDAsm or reflector and it would make things a bit clear. :)

          modified on Monday, June 14, 2010 11:55 AM

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

          d@nish wrote:

          Why and when would you write a code like that?

          There's even a CodeProject-article[^] to answer that one! :-D

          I are Troll :suss:

          D N 2 Replies Last reply
          0
          • D dan sh

            Why and when would you write a code like that? As far as its equivalent is concerned, just see the IL through ILDAsm or reflector and it would make things a bit clear. :)

            modified on Monday, June 14, 2010 11:55 AM

            A Offline
            A Offline
            Al Beback
            wrote on last edited by
            #12

            d@nish wrote:

            As far as its equivalent is concerned, just see the IL through ILDAsm or reflector and it would make things a bit clear.

            I know, but shamefully, I'm IL challenged. :)

            ShamWow

            1 Reply Last reply
            0
            • L Lost User

              Wow, everyone got it wrong :) Debug or Release? The debug code (bugs in Reflector corrected by looking at the MSIL)

              SomeIDisposableClass CS$3$0000;
              bool CS$4$0001;
              CS$3$0000 = new SomeIDisposableClass();
              

              Label_0007:
              try
              {
              SomeCode();
              goto Label_0021;
              }
              finally
              {
              Label_0011:
              CS$4$0001 = CS$3$0000 == null;
              if (CS$4$0001)
              {
              goto Label_0020;
              }
              CS$3$0000.Dispose();
              Label_0020:;
              }
              Label_0021:;

              And yes, it is silly. It's storing the result of the null-check The Release code

              SomeIDisposableClass CS$3$0000;
              CS$3$0000 = new SomeIDisposableClass();
              

              Label_0006:
              try
              {
              SomeCode();
              goto Label_0017;
              }
              finally
              {
              Label_000D:
              if (CS$3$0000 == null)
              {
              goto Label_0016;
              }
              CS$3$0000.Dispose();
              Label_0016:;
              }
              Label_0017:

              edit: the rules for goto-within-try are If the goto statement exits one or more try blocks with associated finally blocks, control is initially transferred to the finally block of the innermost try statement. When and if control reaches the end point of a finally block, control is transferred to the finally block of the next enclosing try statement. This process is repeated until the finally blocks of all intervening try statements have been executed.

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

              harold aptroot wrote:

              Wow, everyone got it wrong Debug or Release?The debug code (bugs in Reflector corrected by looking at the MSIL)

              Bugs? +5 :thumbsup:

              I are Troll :suss:

              L 1 Reply Last reply
              0
              • L Lost User

                harold aptroot wrote:

                Wow, everyone got it wrong Debug or Release?The debug code (bugs in Reflector corrected by looking at the MSIL)

                Bugs? +5 :thumbsup:

                I are Troll :suss:

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

                Bugs like this one.. if ((CS$3$0000 == null) != null) That's not even possible, and the IL for that part is just

                L\_0011: ldloc.0 
                L\_0012: ldnull 
                L\_0013: ceq 
                L\_0015: stloc.1 
                L\_0016: ldloc.1 
                L\_0017: brtrue.s L\_0020
                

                So Mr Reflector just skipped over a load/store and made up a comparison :wtf:

                1 Reply Last reply
                0
                • L Lost User

                  Wow, everyone got it wrong :) Debug or Release? The debug code (bugs in Reflector corrected by looking at the MSIL)

                  SomeIDisposableClass CS$3$0000;
                  bool CS$4$0001;
                  CS$3$0000 = new SomeIDisposableClass();
                  

                  Label_0007:
                  try
                  {
                  SomeCode();
                  goto Label_0021;
                  }
                  finally
                  {
                  Label_0011:
                  CS$4$0001 = CS$3$0000 == null;
                  if (CS$4$0001)
                  {
                  goto Label_0020;
                  }
                  CS$3$0000.Dispose();
                  Label_0020:;
                  }
                  Label_0021:;

                  And yes, it is silly. It's storing the result of the null-check The Release code

                  SomeIDisposableClass CS$3$0000;
                  CS$3$0000 = new SomeIDisposableClass();
                  

                  Label_0006:
                  try
                  {
                  SomeCode();
                  goto Label_0017;
                  }
                  finally
                  {
                  Label_000D:
                  if (CS$3$0000 == null)
                  {
                  goto Label_0016;
                  }
                  CS$3$0000.Dispose();
                  Label_0016:;
                  }
                  Label_0017:

                  edit: the rules for goto-within-try are If the goto statement exits one or more try blocks with associated finally blocks, control is initially transferred to the finally block of the innermost try statement. When and if control reaches the end point of a finally block, control is transferred to the finally block of the next enclosing try statement. This process is repeated until the finally blocks of all intervening try statements have been executed.

                  A Offline
                  A Offline
                  Al Beback
                  wrote on last edited by
                  #15

                  That's what I was looking for, thanks!

                  ShamWow

                  1 Reply Last reply
                  0
                  • L Lost User

                    d@nish wrote:

                    Why and when would you write a code like that?

                    There's even a CodeProject-article[^] to answer that one! :-D

                    I are Troll :suss:

                    D Offline
                    D Offline
                    dan sh
                    wrote on last edited by
                    #16

                    Nice find. :) From now on, I am going to have this in my windows forms application (of course with proper comments):

                    public class AppCursor : IDisposable
                    {

                        Cursor \_currentCursor = null;
                        bool \_setDefault;
                    
                        public AppCursor(Cursor cursor, bool setDefault)
                        {
                            \_currentCursor = Cursor.Current;
                            \_setDefault = setDefault;
                            Cursor.Current = cursor;
                        }
                    
                        #region IDisposable Members
                    
                        public void Dispose()
                        {
                            Cursor.Current = (\_setDefault) ? Cursors.Default : \_currentCursor;
                            
                            // Even with disposing the cursor object, form works but am not sure whether it should be there or not.
                            \_currentCursor.Dispose();
                            \_currentCursor = null;
                        }
                    
                        #endregion
                    }
                    
                    1 Reply Last reply
                    0
                    • L Lost User

                      d@nish wrote:

                      Why and when would you write a code like that?

                      There's even a CodeProject-article[^] to answer that one! :-D

                      I are Troll :suss:

                      N Offline
                      N Offline
                      Not Active
                      wrote on last edited by
                      #17

                      Chris should put that in the advertising. There is an app article for that. :-D


                      I know the language. I've read a book. - _Madmatt

                      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