Use of Finalize vs Dispose in .NET

๐Ÿ’ก Concept Name

Finalize vs Dispose in .NET

๐Ÿ“˜ Quick Intro

Both Finalize() and Dispose() are used for releasing unmanaged resources in .NET. Finalize is invoked by the GC, while Dispose is called explicitly by the developer.

๐Ÿง  Analogy / Short Story

Imagine leaving a hotel. You can leave your room and wait for the hotel staff to clean it (Finalize), or you can clean it yourself before leaving (Dispose). Dispose gives you control and avoids burdening housekeeping (GC).

๐Ÿ”ง Technical Explanation

Finalize() is a protected method automatically called by the garbage collector when an object is no longer reachable. You override it to clean unmanaged resources.

Dispose() is a public method from the IDisposable interface. It must be called manually (or via using) to free unmanaged resources deterministically.

๐ŸŽฏ Purpose & Use Case

  • โœ… Use Dispose() for deterministic cleanup
  • โœ… Use Finalize() only when Dispose isn't called
  • โœ… Use both in combination via Dispose Pattern
  • โœ… Always prefer using statement for IDisposable objects

๐Ÿ’ป Real Code Example

public class FileManager : IDisposable
{
    private FileStream _stream;

    public FileManager(string path)
    {
        _stream = new FileStream(path, FileMode.Open);
    }

    public void Dispose()
    {
        _stream?.Dispose();
        GC.SuppressFinalize(this); // avoid finalizer call
    }

    ~FileManager()
    {
        Dispose();
    }
}

โ“ Interview Q&A

Q1: What is Finalize() used for?
A: Cleanup of unmanaged resources during garbage collection.

Q2: What is Dispose() used for?
A: Manual cleanup of unmanaged resources.

Q3: Which one is deterministic โ€” Finalize or Dispose?
A: Dispose.

Q4: What does GC.SuppressFinalize() do?
A: Prevents the Finalize call for an object.

Q5: Can we override Dispose()?
A: Yes, in classes implementing IDisposable.

Q6: When is Finalize called?
A: By GC before reclaiming memory.

Q7: What pattern uses both Finalize and Dispose?
A: Dispose Pattern.

Q8: Why prefer Dispose over Finalize?
A: For performance and control.

Q9: Should Dispose call GC.SuppressFinalize?
A: Yes, if Finalize is also implemented.

Q10: Can we use using() with Finalize?
A: No, only with IDisposable/Dispose.

๐Ÿ“ MCQs

Q1. Which method is called by the garbage collector?

  • Dispose
  • Free
  • Release
  • Finalize

Q2. Which method must be called manually?

  • Finalize
  • Collect
  • Dispose
  • CleanUp

Q3. What interface must a class implement to support Dispose()?

  • IGC
  • IReleasable
  • IDisposable
  • IManualCleanup

Q4. Which method is deterministic?

  • Finalize
  • Dispose
  • Both
  • None

Q5. Which method is safe to use in using() block?

  • Dispose
  • Finalize
  • Close
  • Abort

Q6. What does GC.SuppressFinalize() do?

  • Forces GC
  • Delays finalizer
  • Prevents GC from calling Finalize
  • Removes IDisposable

Q7. When should Finalize() be used?

  • Always
  • Never
  • Only with IDisposable
  • When unmanaged resources need fallback cleanup

Q8. What is the best practice for unmanaged resources?

  • Only Finalize
  • Only Dispose
  • No cleanup needed
  • Implement Dispose and optionally Finalize

Q9. Is Dispose() called automatically?

  • Yes
  • No
  • Only with GC
  • Only on crash

Q10. Which causes a performance hit if not suppressed?

  • Dispose
  • using()
  • GC.Collect()
  • Finalize

๐Ÿ’ก Bonus Insight

Use the using statement with all IDisposable objects like FileStream, SqlConnection, etc., to ensure automatic and reliable cleanup. This is safer and more performant than relying on Finalize.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: