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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:51:42+00:00 2026-05-16T00:51:42+00:00

When working with files in C#, I am conditioned to think about freeing the

  • 0

When working with files in C#, I am conditioned to think about freeing the associated resources. Usually this is a using statement, unless its a one liner convenience method
like File.ReadAllLines, which will open and close the file for me.

.Net 4.0 has introduced the convenience method File.ReadLines. This returns an IEnumerable and is billed as a more efficient way to process a file – it avoids storing the entire file in memory. To do this I’m assuming there is some deferred execution logic in the enumerator.

Obviously since this method returns an IEnumerable, not and IDisposable, I can’t go with my gut reaction of a using statement.

My questions is:
With this in mind, are there any gotchas on resource deallocation with this method?

Does calling this method mean that the release of the associated file locks is non-deterministic?

  • 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-16T00:51:42+00:00Added an answer on May 16, 2026 at 12:51 am

    IEnumerable doesn’t inherit from IDisposable because typically, the class that implements it only gives you the promise of being enumerable, it hasn’t actually done anything yet that warrants disposal.

    However, when you enumerate over it, you first retrieve an IEnumerator by calling the IEnumerable.GetEnumerator method, and typically, the underlying object you get back does implement IDisposable.

    The way foreach is implemented is similar to this:

    var enumerator = enumerable.GetEnumerator();
    try
    {
        // enumerate
    }
    finally
    {
        IDisposable disposable = enumerator as IDisposable;
        if (disposable != null)
            disposable.Dispose();
    }
    

    This way, if the object does indeed implement IDisposable, it will be disposed of. For File.ReadLines, the file isn’t really opened until you start enumerating over it, so the object you get from File.ReadLines doesn’t need disposing, but the enumerator you get, does.

    As the comments indicate, IEnumerator does not inherit from IDisposable, even though many typical implementations does, whereas the generic IEnumerator<T> does inherit IDisposable.

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

Sidebar

Related Questions

Does anyone have any useful advice/links to information about working with mp3 files in
Using this upload script and it was working ok a week ago but when
i'm new to the realm to working with Files in .NET I'm creating a
Basically my rewrite rules work fine but if I am working with files or
We've been working on some files at the same time. Occasionally, when we svn
I have been working with resource files for a while now and I was
I know there are libraries out there for working with ZIP files . And,
I am working on splitting code into smaller files and refactoring it a bit.
I'm working on an editor for files that are used by an important internal
I'm working on a database that tracks files and dependencies in projects. Briefly, I

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.