TagIDisposable interface

Finalize method or Destructor

Finalize method or Destructor are synonyms. We create destructor by typing ~ (tilde sign) and class name. Use of destructor is to cleanup unamaged resources when Garbage collector runs. It is a good practice that your destructor calls Dispose method and provides it with false boolean value. Why with false value? Because First Dispose method is called by your code and provides second Dispose method with true value, meaning only managed code will be disposed of, and by providing Dispose of method with false method we ensure that garbage collector will not try to destroy of already disposed of objects.

Destructor can be called in two ways:

  1. When Garbage collector runs by itself
  2. When we invoke Garbage Collector by using GC.Collect()

Implementing IDisposable

As we saw in previous post, we can use using statement to automatically dispose of objects. To use using directive our class needs to implement IDisposable interface.  To correctly implement IDisposable interface we should stick to some guidelines. Following example will show you the correct way to implement it.

Example of Implementing IDisposable interface to a class

We will start by creating FileRead class whose job is to create file and write line to it. FileRead class will implement IDispose interface and two Dispose methods. General rule is that you should create two Dispose methods:

  • public void method with no parameters
  • protected void method with a boolean input parameter

Job of public Dispose method is to call second Dispose method where objects are really disposed of, and second job is to notify Garbage collector that this instance of class is cleaned up and that there is no need to check-up upon it during garbage collection.

More examples of IDisposable coming up soon.

Garbage Collector

Have you ever wondered how applications know how much memory to use, how is that memory distributed or who cleans up memory? All those operations are executed by the Garbage Collector.

What triggers Garbage collector?

  • Your system is low on physical memory.  Meaning your system has used up almost all of your RAM memory and will start to use page file.
  • The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs.
  • Application is written to call the GC.Collect method.

Continue reading

Using keyword

Before we continue to FileInfo and Streams we need to explain using keyword in details.

Using directive

Using directive shortens our syntax so that we do not always have to type entire namespace:

Using directive grants us access to static members having to qualify the access with the type name:

Using directive enables us to create alias directive.

Using statement

Using statement provides us with a easy to use syntax which in return guarantees us with that types that implement IDisposable interface will be disposed of when we finish with them. For now do not stress yourself with questions like why do I need to dispose of something and what use IDisposable interface is to me. Those questions will be covered in a future post.

Example of using statement

Usage of using statement is straight-forward.

First we type using keyword and open parentheses, and inside of those brackets we instantiate class that implements IDisposable interface.  After that we proceed to use class instance inside curly brackets.

Of-course  you can instantiate more than one class while using using directive, just remember to separate them with coma:

I hope you will find using keyword helpful. I certainly use it on a daily basis in both forms

© 2021 LearnC#WithDarko

Theme by Anders NorenUp ↑