Tagusing statement

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.

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

File and FileInfo

While trying to copy, move, rename, create, open, delete and appending to files we use File and FileInfo classes. File and File info classes derive from System.IO namespace. File class provides us static methods to work with files and FileInfo provides us with instance methods. Generally, you will use File class when working with a file and perform one operation on it. Difference between File and FileInfo approaches is that FileInfo does security checking only once (while accessing files).  Because FileInfo is a instance method it does not tack changes to a file so you will have to take a lot of precautions while using that class. File class is on a disadvantage, because you always have to apply path to a file, meaning, it takes more parameters than FileInfo class. Try discovering yourself which approach is faster in your scenario. Also both classes help us while creating FileStream (to be explained in next post).

Common operations working with File class


Common operations working with FileInfo  class


I hope you had no problem with learning how to use File and FileInfo classes. If you found File and FileInfo classes interesting I recommend that you continue learning about Streams.

© 2021 LearnC#WithDarko

Theme by Anders NorenUp ↑