shunman
shunman

Reputation: 267

C# Do I use dispose() correctly?

I studying C# IDisposable now at ASP.NET MVC I implemented code like this,

using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace DisposeTest.Controllers
{
    public class TestClass : IDisposable
    {
        private bool disposed = false;

        private Component component = new Component();
        private List<string> dataItem = new List<string>();

        ~TestClass()
        {
            Dispose(disposing: false);
        }

        public void Dispose()
        {
            Dispose(true);

            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {

                if (disposing)
                {
                    // DISPOSE MANAGED RESOURCE
                    component.Dispose();
                }

                // DISPOSE UNMANAGED RESOURCE
                dataItem = null;

                disposed = true;
            }
        }

        public void AddItem()
        {
            dataItem.Add("wwwwwwwwwwwwwww");
        }
    }
}
using System.Web.Mvc;

namespace DisposeTest.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            TestClass tc = new TestClass();

            tc.AddItem();
            tc.dispose();
            return View();
        }
    }
}

After When I push F5 to launch Visual studio debugger with web browser, debugger catch into destructor. But after I refreshed web browser again or hit enter browser address, debugger did not catch destructor.

I would like to know some answers.

  1. ASP.NET MVC call destructor after every controller action?
  2. Do I correctly used Dispose()?
  3. Should I clear variables even private member field?

Thank you for your your adivices

ps. I added to call dispose() method after doing add();

Upvotes: 0

Views: 901

Answers (1)

Aarnihauta
Aarnihauta

Reputation: 540

For example. We have CatDataStore class who works with data base and implements IDisposable. This is full implements IDisposable:

class CatDataStore : IDisposable
{
    //data context contains models, connection string... etc
    private ApplicationDbContext _context = new ApplicationDbContext();
    private bool disposed;

    public async Task<bool> AddItem(Cat item)
    {
        await _context.Cats.AddAsync(item);
        await _context.SaveChangesAsync();
        return true;
    }
    //smth CRUD methods..

    
    public void Dispose()
    {
        Dispose(true);
        
        //GC can't eat this
        GC.SupressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (disposed)
                throw new ObjectDisposedException();
            _context.Dispose();
        }
        disposed = true;
    }

    ~CatDataStore() => Dispose(false);
}

Upvotes: 1

Related Questions