0

This is a made up example to demonstrate my question better. I've been using this Entity class for a while now so that my entity classes can be make use of "using" blocks. But to be honest I don't know what should go in the Dispose(bool disposing) method. This code all works fine but like I said, I'm not sure what to use the method for.

Thanks

void Main()
{
    using (Person p = new Person())
    {
        p.First="Stan";
        p.Last="Smith";
        SayHello(p);
    }
}

public abstract class Entity : IDisposable
{
    public virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            // Add logic here for common dispose. 
        }
    }
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

public class Person : Entity 
{
    public string First { get; set; }

    public string Last { get; set; }
}

public void SayHello(Person p)
{
    Console.WriteLine(@"Hello {0} {1}", p.First, p.Last);
}
Dave
  • 313
  • 1
  • 7
  • 18
  • possible duplicate? [link](https://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface) – Ali Vahidinasab Apr 12 '19 at 18:43
  • 2
    That's where you would dispose of any unmanaged (and possibly managed also, if you like) resources you've created. As your code currently stands, `Entity` has nothing to dispose. – Rufus L Apr 12 '19 at 18:51
  • Ok so as there's no resources to dispose of then the "using" is pointless? The person who taught me C# always said to use "using" wherever possible but this just seems like redundant code and perhaps following a pattern for the sake of it. – Dave Apr 12 '19 at 22:20

0 Answers0