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 1035275
In Process

The Archive Base Latest Questions

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

What is better, the using directive, or the dispose directive when finished with an

  • 0

What is better, the using directive, or the dispose directive when finished with an object?

 using(FileStream fileStream = new FileStream(
            "logs/myapp.log",
            FileMode.Open,
            FileAccess.Read,
            FileShare.ReadWrite))
        {
            using(StreamReader streamReader = new StreamReader(fileStream))
            {
                this.textBoxLogs.Text = streamReader.ReadToEnd();
            }
        }

On the other hand, when I’m dealing with System.Net.Mail, I’m told I need to Dispose() of the object to release any stray locks.

Is there any consistent guidance? How do I tell what is more appropriate in a given situation for a given object?

  • 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:31:06+00:00Added an answer on May 16, 2026 at 2:31 pm

    There’s no reason that I can think of to manually call Dispose(), other than in another implementation of Dispose() (for example in a class you’ve created that implements IDisposable) when you can wrap an object in a using block. The using block puts the creation and disposal of the object in a try/catch/finally block to pretty much gaurantee that the object will be disposed of correctly.

    The compiler is more reliable than me. Or you. =)

    MSDN documents the using statement and calls out where you can obtain the C# language specification where you can review section 8.13 “The using statement” (at least in the v4.0 reference it’s 8.13) that gives a comprehensive explanation of the using statement and how to use it. The fifth paragraph gives the following:

    A using statement is translated into
    three parts: acquisition, usage, and
    disposal. Usage of the resource is
    implicitly enclosed in a try statement
    that includes a finally clause. This
    finally clause disposes of the
    resource. If a null resource is
    acquired, then no call to Dispose is
    made, and no exception is thrown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a REST- Server here using JERSEY. I must log the IP (better
I wanted others to explain to me, which approach is better: using a session
Is there a better method (using Windows) for getting a moderate amount of data
I know their are faster\better ways using Interlocked.CompareExchange but I'm just looking for the
I am now using asp.net mvc and wondering what is a better choice using
Is there a better way than using globals to get interesting values from a
Is there a better way to shell sort using C#? // array of integers
Is there better use of available storage space and faster network access using the
What is a better option (in terms of performance): copying a file using fileinputstream
What is a better approach to serialize custom class: using XMLSerializer or BinarryFormatter and

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.