Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1036345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:41:20+00:00 2026-05-16T14:41:20+00:00

This is a followup question to this Should I stick with the Try/Catch/Finally construct,

  • 0

This is a followup question to this

Should I stick with the Try/Catch/Finally construct, or go with the Using construct?

Sample Code for Try/Catch/Finally:

Dim oRequest As WebRequest
Dim oResponse As HttpWebResponse = Nothing
Dim dataStream As Stream = Nothing
Dim reader As StreamReader = Nothing
Dim responseFromServer As String

Try
        sNewCustomerURL = NewCustomerQueryStringPrepare()

    'make the call to the webservice to add a new customer
    oRequest = WebRequest.Create(sNewCustomerURL)

    oRequest = CType(oRequesC, HttpWebRequest)
    oRequest.Method = "GET"
    oResponse = CType(oRequest.GetResponse(), HttpWebResponse)

    dataStream = oResponse.GetResponseStream()
    reader = New StreamReader(dataStream)
    responseFromServer = reader.ReadToEnd()

        Dim xml As New XmlDocument()
    xml.LoadXml(responseFromServer)
    Dim node As XmlNodeList = xml.GetElementsByTagName("SUCCESS")
    Dim value = CBool(node(0).InnerText)

    'do stuff               


Catch ex As Exception

       'process exception

Finally

    'do cleanup
    oRequest = Nothing
    If Not oResponse Is Nothing Then
        oResponse.Close()
    End If
    oResponse = Nothing
    If Not reader Is Nothing Then
        reader.Close()
    End If
    reader = Nothing
    If Not dataStream Is Nothing Then
        dataStream.Flush()
        dataStream.Close()
    End If
    dataStream = Nothing
End Try

I know what the code would need to be for the Using construct. I just want to know if using the Using construct would be faster comparing clock cycles.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:41:20+00:00Added an answer on May 16, 2026 at 2:41 pm

    There won’t be a performance difference. using is expanded by the compiler to a try/finally block.

    You will see that the following two methods compile to identical IL.

    void SampleWithUsing()
    {
        using (MemoryStream s = new MemoryStream())
        {
            s.WriteByte(1);
        }
    }
    
    void SampleWithTryFinally()
    {
        MemoryStream s = new MemoryStream();
        try
        {
            s.WriteByte(1);
        }
        finally
        {
            if (s != null) s.Dispose();
        }
    }
    

    The IL generated in the first case is:

    .method private hidebysig instance void  SampleWithUsing() cil managed
    {
      // Code size       26 (0x1a)
      .maxstack  2
      .locals init ([0] class [mscorlib]System.IO.MemoryStream s)
      IL_0000:  newobj     instance void [mscorlib]System.IO.MemoryStream::.ctor()
      IL_0005:  stloc.0
      .try
      {
        IL_0006:  ldloc.0
        IL_0007:  ldc.i4.1
        IL_0008:  callvirt   instance void [mscorlib]System.IO.Stream::WriteByte(uint8)
        IL_000d:  leave.s    IL_0019
      }  // end .try
      finally
      {
        IL_000f:  ldloc.0
        IL_0010:  brfalse.s  IL_0018
        IL_0012:  ldloc.0
        IL_0013:  callvirt   instance void [mscorlib]System.IDisposable::Dispose()
        IL_0018:  endfinally
      }  // end handler
      IL_0019:  ret
    } // end of method Program::SampleWithUsing
    

    In the second case with a try/finally in C# we get:

    .method private hidebysig instance void  SampleWithTryFinally() cil managed
    {
      // Code size       26 (0x1a)
      .maxstack  2
      .locals init ([0] class [mscorlib]System.IO.MemoryStream s)
      IL_0000:  newobj     instance void [mscorlib]System.IO.MemoryStream::.ctor()
      IL_0005:  stloc.0
      .try
      {
        IL_0006:  ldloc.0
        IL_0007:  ldc.i4.1
        IL_0008:  callvirt   instance void [mscorlib]System.IO.Stream::WriteByte(uint8)
        IL_000d:  leave.s    IL_0019
      }  // end .try
      finally
      {
        IL_000f:  ldloc.0
        IL_0010:  brfalse.s  IL_0018
        IL_0012:  ldloc.0
        IL_0013:  callvirt   instance void [mscorlib]System.IO.Stream::Dispose()
        IL_0018:  endfinally
      }  // end handler
      IL_0019:  ret
    } // end of method Program::SampleWithTryFinally
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A followup to this previous question: Current code: var query = from b in
This is a followup on my previous question , I am using the 2to3
This is a follow-up to the question: Should the folders in a solution match
This is a followup question of How to encode characters from Oracle to Xml?
This is a followup question to my other widget-related question . I'd like to
This is a followup question to my other question : Run bat file in
This is a followup to this question . I seem to be stuck on
This is a followup on the question: ASP.NET next/previous buttons to display single row
This is a followup to a question I posted yesterday. I thought everything was
As kind of a followup to this question I've gotten a solution working on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.