-1

In the example below... Since the class have an IDisposable interface and the class is itself is only a manage memory objects (not an unmanaged memory), so in the script further down below (See case #1 and case #2) - what is the proper way to do Disposal when done w/ the list objects?

 public class VehicleA : IDisposable
 {
     public void Dispose() { }

     public string Year {get;set;}
     public string Make {get;set;}
     public string Model {get;set;}
 }
 public class repositoryVehicle()
 {
     public List<VehicleA> VehicleLookup()
     {
         List<VehicleA> returnVehicles = new List<Vehicle>();

         returnVehicles.Add(new VehicleA { Year="2007", Make="Ford", Model="Mustang" });
         returnVehicles.Add(new VehicleA { Year="2004", Make="Chevy", Model="Blazer" });

         return returnVehicles;
     }
 }

 //Case #1...
 foreach(var v in repositoryVehicle.VehicleLookup())
 {
     //v.Year...
 }

 //Case #2...
 List<VehicleA> vehicles = new List<VehicleA>();
 vehicles = repositoryVehicle.VehicleLookup();
 //vehicles[x].Year...
fletchsod
  • 3,560
  • 7
  • 39
  • 65
  • 9
    If your class doesn't have anything that needs special cleanup, why have it implement IDisposable? – Tyler Lee Jul 16 '13 at 21:39
  • 3
    Possibly relevant: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface – Chris Jul 16 '13 at 21:39
  • take a look at this SO Q&A on [when to use IDisposable with managed resources](http://stackoverflow.com/questions/3908463/idisposable-interface) – jltrem Jul 16 '13 at 21:39
  • What @nekizalb said: unless you're connecting to a DB, or doing something that's not going to be quickly and automatically de-allocated or closed, there's no real reason to implement IDisposable. – Garrison Neely Jul 16 '13 at 21:40
  • Your classes do not need `IDisposable`. If this was meant as pseude ocode you'll have to highlight that and the actual question better. – H H Jul 16 '13 at 21:42

1 Answers1

2

The dispose pattern is used only for objects that access unmanaged resources. This is because the garbage collector is very efficient at reclaiming unused managed objects.

There is no performance benefit in implementing the Dispose method on types that use only managed resources (such as arrays) because they are automatically reclaimed by the garbage collector. Use the Dispose method primarily on managed objects that use native resources and on COM objects that are exposed to the .NET Framework. Managed objects that use native resources (such as the FileStream class) implement the IDisposable interface.

http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx

Fals
  • 6,813
  • 4
  • 23
  • 43
Pushpendra
  • 814
  • 1
  • 6
  • 17
  • Array and `List<>` are not managed resources. They're just memory. – H H Jul 16 '13 at 21:43
  • "*This is because the garbage collector is very efficient at reclaiming unused managed objects.*" I'd like to qualify that. GC is efficient only if there is enough memory. For example see here: http://sealedabstract.com/rants/why-mobile-web-apps-are-slow/ – Andrew Savinykh Jul 16 '13 at 21:45
  • 1
    *"The dispose pattern is used only for objects that access unmanaged resources"* - I've also used `IDisposable` to make sure events are correctly unsubscribed to, or else they could cause memory leaks. I consider that a design-flaw with events, though - events subscriptions *should* be a weak-reference. – BlueRaja - Danny Pflughoeft Jul 16 '13 at 21:46
  • @BlueRaja-DannyPflughoeft yes, this is certainly a relevant point. I'm not sure that event subscribtion *should* be weak-reference, but it's true that if you subscribe to something and let the subscriber go the memory won't be reclaimed as the subscriber will continue processing events. – Andrew Savinykh Jul 16 '13 at 21:48
  • 1
    @zespri: ...that article *(from your first comment)* is about JS garbage collection in various browsers, which has absolutely nothing to do with this question or answer... – BlueRaja - Danny Pflughoeft Jul 16 '13 at 21:49
  • @zespri: (re: second comment) I am talking about the event publisher not being reclaimed, despite the fact that it will never publish any more events. – BlueRaja - Danny Pflughoeft Jul 16 '13 at 21:51
  • @BlueRaja-DannyPflughoeft, no, it's not the case. a) GC in general have the same principles JS or not, and more importantly b) the article also talks about .net on mobile devices. – Andrew Savinykh Jul 16 '13 at 21:51
  • Mr BlueRaja...pls read the article from Microsoft. I am sure it will clear all your queries. – Pushpendra Jul 16 '13 at 21:53
  • @Purshpendra - "Use the Dispose method primarily on managed objects that use native resources". In other word, you're trying to say like this... FileStream.Dispose(); Right? – fletchsod Jul 16 '13 at 22:05
  • 1
    If anything that is Managed object will be dealt by GC and you need to implement Disposable when and only when you have anything that is unmanaged object. – Pushpendra Jul 16 '13 at 22:09