Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your question really seems to boild down to 'How do I delete stuff in C#'. The short answer is you can't, that's the job of the Garbage Collector <code>System.GC</code>. The <code>IDisposable</code> inerface is used to ensure that imporant resources <em>that don't belong to .Net</em> (like files, or network/database connections, but <em>not</em> a .Net class), are cleaned up on demand.</p> <p>The <code>IDisposable</code> interface is important because it allows you to use the <code>using</code> pattern. That is, if your class implements <code>IDisposable</code> it can be used in a <code>using</code> block. Consider for instance:</p> <pre><code>class MyClass : IDisposable { public void Dispose() { } } </code></pre> <p>this class can now be used like this:</p> <pre><code>using (MyClass instance = new MyClass()) { } // instance.Dispose() is called here at the end of the block. </code></pre> <p>The point of the using block is that when the block ends, it the compiler will throw in a call to <code>Dispose()</code> which you can use to get rid of important resources like files and database connections. For example, all of the <code>Stream</code> classes, like <code>FileStream</code> and what not implement <code>IDisposable</code> because it's a bad idea to leave a file open. Instead, you wrap all of your access in a <code>using</code> block, and then you are guaranteed that <code>FileStream.Dispose</code> will close the file out. Consider:</p> <pre><code>using (FileStream myFile = File.OpenRead("...")) { // Read the content of the file. } // The file is guaranteed to be closed here. Cool! </code></pre> <p>This is much neater than doing something like this:</p> <pre><code>FileStream stream = File.OpenRead(" ... "); stream.Close(); // Yes, you closed it manually, but it's error prone. What if you forget to do this? </code></pre> <p>Now what you're thinking of is a term called "Finalization", that is when the class is actually destroyed. This happens when the garbage collector (the <code>System.GC</code> class) actually destroys objects and cleans up their memory. Consider:</p> <pre><code>public class MyClass { // This method, the 'Finalizer' will be called when the class is destroyed. // The 'finalizer' is essentially just the name of the class with a '~' in front. ~MyClass() { Console.WriteLine("Destroyed!"); } } public class Program { public static void Main() { MyClass referenceHeld = new MyClass(); // Reference held new MyClass(); // No reference held on this class WeakReference sameAsNoReference = new WeakReference(new MyClass()); // Equivalent to no reference. System.GC.Collect(); // Force the garbage collector to collect Console.ReadLine(); } } </code></pre> <p>In short, the Garbage collector is the part of the runtime that cleans up stuff that isn't being used. What does it mean to not be used? It means that there are no references attached to the object. For example, if you run the program above, you'll notice that the word "Destroyed" gets printed on the screen twice. That's because two of the instances of <code>MyClass</code> created in the <code>Main</code> function are not pointed to by reference (A <code>WeakReference</code> is essentially the same thing as no reference). When we call <code>GC.Collect()</code> the garbage collector runs and cleans up the references.</p> <p>That said, you should <strong>NOT</strong> call <code>GC.Collect</code> on your own. You can, for experimentation and education of course, but most people will tell you that the garbage collector does a fine job of keeping things clean on it's own. It doesn't make sense to have a bunch of <code>GC.Collect</code> scattered throughout your code, because that's the whole point of having a Garbage collector - to not have to worry about cleaning things up yourself.</p> <p>So in short, you really can't destroy objects on your own, unless you call <code>GC.Collect()</code> (which you shouldn't do). The <code>IDisposable</code> interface allows you to work with the <code>using</code> pattern, ensuring that important resources are released (This is not the same thing as destroying the object though! All <code>IDisposable</code> does is ensure that <code>Dispose()</code> is called when the <code>using</code> block exits so you can clean up important stuff, but the object is still alive - an important distinction).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload