Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Why is it important to know that using has no empty catch and check for null in the finally?VB.Net make one thread wait for the next now with Async Await »
    comments

    Introduction

    Yesterday there was a question on the VBIB forum on what the Using statement does and I answered it. But before going on I checked if I was right by looking at this MSDN article about Using.

    And it states.

    1. ' THE FOLLOWING TRY CONSTRUCTION IS EQUIVALENT TO THE USING BLOCK
    2. Dim resource As New resourceType
    3. Try
    4.     ' Insert code to work with resource.
    5. Catch ex As Exception
    6.     ' Insert code to process exception.
    7. Finally
    8.     ' Insert code to do additional processing before disposing of resource.
    9.     resource.Dispose()
    10. End Try

    That is the 2005 version of that article. The 2010 and 2012 version seem to have a different opinion.

    1. ' For the acquisition and disposal of resource, the following
    2. ' Try construction is equivalent to the Using block.
    3. Dim resource As New resourceType
    4. Try
    5.     ' Insert code to work with resource.
    6. Finally
    7.     If resource IsNot Nothing Then
    8.         resource.Dispose()
    9.     End If
    10. End Try

    Another user of VBIB corrected this saying that there is no catch.

    So I set out to check and downloaded Teleriks JustDecompiler.

    TestData

    First thing I did was to create a project. with this snippet in it.

    1. Imports System.IO
    2.  
    3. Module Module1
    4.  
    5.     Sub Main()
    6.         Using s1 = New FileStream("test1", FileMode.Create)
    7.  
    8.         End Using
    9.     End Sub
    10.  
    11. End Module

    Decompilation

    And I then decompiled that.

    In IL that would look something like this.

    1. IL_0000: nop
    2.     IL_0001: nop
    3.     IL_0002: ldstr "test1"
    4.     IL_0007: ldc.i4.2
    5.     IL_0008: newobj instance void [mscorlib]System.IO.FileStream::.ctor(string,  valuetype [mscorlib]System.IO.FileMode)
    6.     IL_000d: stloc.2
    7.     IL_000e: nop
    8.     .try
    9.     {
    10.         IL_000f: nop
    11.         IL_0010: leave.s IL_0028
    12.     }
    13.     finally
    14.     {
    15.         IL_0012: ldloc.2
    16.         IL_0013: ldnull
    17.         IL_0014: ceq
    18.         IL_0016: ldc.i4.0
    19.         IL_0017: ceq
    20.         IL_0019: stloc.s VB$CG$t_bool$S0
    21.         IL_001b: ldloc.s VB$CG$t_bool$S0
    22.         IL_001d: brfalse.s IL_0026
    23.  
    24.         IL_001f: ldloc.2
    25.         IL_0020: callvirt instance void [mscorlib]System.IDisposable::Dispose()
    26.         IL_0025: nop
    27.  
    28.         IL_0026: nop
    29.         IL_0027: endfinally
    30.     }

    and in C# that would recompile to this.

    1. FileStream s1 = new FileStream("test1", FileMode.Create);
    2.     try
    3.     {
    4.     }
    5.     finally
    6.     {
    7.         bool flag = s1 != null;
    8.         if (flag)
    9.         {
    10.             s1.Dispose();
    11.         }
    12.     }

    And in VB.Net to this.

    1. Using s1 As FileStream = New FileStream("test1", FileMode.Create)
    2.         If (s1 = Nothing) Then
    3.         End If
    4.     End Using


    I kinda think that the C# version of the decompilation is closer to the truth.

    Conclusion

    Did I get the wrong information from that MSDN article or was it different back in 2005? I have no idea. I just know that the 2010 and 2012 versions of that article are closer to the truth.

    And don't trust you decompiler to tell you the complete truth either ;-).

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    using, vb.net
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)