Strange thing about .NET 4.0 filesystem enumeratation functionality

Posted by codymanix on Stack Overflow See other posts from Stack Overflow or by codymanix
Published on 2010-04-18T19:17:33Z Indexed on 2010/04/18 19:23 UTC
Read the original article Hit count: 518

I just read a page of "Whats new .NET Framework 4.0". I have trouble understanding the last paragraph:

To remove open handles on enumerated directories or files

  1. Create a custom method (or function in Visual Basic) to contain your enumeration code.

  2. Apply the MethodImplAttribute attribute with the NoInlining option to the new method. For example:

    [MethodImplAttribute(MethodImplOptions.NoInlining)] Private void Enumerate()

  3. Include the following method calls, to run after your enumeration code:

      * The GC.Collect() method (no parameters).
      * The GC.WaitForPendingFinalizers() method.
    

Why the attribute NoInlining? What harm would inlining do here?

Why call the garbage collector manually, why not making the enumerator implement IDisposable in the first place? I suspect they use FindFirstFile()/FindNextFile() API calls for the imlementation, so FindClose() has to be called in any case if the enumeration is done.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about .net4