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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:09:53+00:00 2026-06-15T06:09:53+00:00

Looking at ‘System.IO.File.AppendAllText’ code from the GAC it calls another method called ‘InternalAppendAllText’ which

  • 0

Looking at ‘System.IO.File.AppendAllText’ code from the GAC it calls another method called ‘InternalAppendAllText’ which creates a new StreamWriter and writes the content to the file.

//mscorlib, System.IO
private static void InternalAppendAllText(string path, string contents, Encoding encoding)
{
    using (StreamWriter writer = new StreamWriter(path, true, encoding))
    {
      writer.Write(contents);
    }
}

My question is if I did a for loop calling System.IO.AppentAllText 5 times for example will the StreamWriter be created 5 times, each iteration it is initialized and disposed or it is only initialized once?

Example:

for(int i = 1; i < 4; ++i)
{
    System.IO.File.AppendAllText("a", "a");
}
  • 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-06-15T06:09:54+00:00Added an answer on June 15, 2026 at 6:09 am

    The using block is expanded to the equivalent of this code:

    StreamWriter writer;
    try
    {
        writer = new StreamWriter(path, true, encoding))
        writer.Write(contents);
    }
    finally
    {
        if (writer != null)
             writer.Dispose();
    }
    

    So yes, whenever a using block exits, the objects are disposed. Whenever you reenter the function, a new StreamWriter will be initialized.

    Note, however, that this doesn’t have anything to do with the Garbage Collector. The GC will run when it feels like it, scan the allocated objects, notice that there are five StreamWriter objects that aren’t attached to any GC Root, and discard them. This is true whether or not you called Dispose or used using, it’s just how the GC works.

    What using does here is release the unmanaged resource that the object holds – in this case, the file handle. If the StreamWriter was not destroyed or otherwise explicitly closed, the file would have remained locked for editing until your process has finished or the GC decided to activate. This is what using blocks and the IDisposable interace are for – to make sure you explicitly release the objects that the GC doesn’t know about.

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

Sidebar

Related Questions

Looking at some of the code System.Linq I've come across some examples of Buffer<TSource>
Looking for C# class which wraps calls to do the following: read and write
Looking for suggestions on file system management tools. We have several terabytes of images,
Looking for a way to enable someone to upload a single file which will
Looking at the last JUnit test case I wrote, I called log4j's BasicConfigurator.configure() method
Looking at some code written by another developer, I came across this: for($i=1; $i<=30;
Looking at some assembly code for x86_64 on my Mac, I see the following
Looking for a way to get path of file (and then process it in
Looking for a better way of extracting a number from the last line in
Looking for good explanation of why this code raises SyntaxError . def echo(x): return

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.