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.


Constructors are a public methods with the same name as a class were they resides. Constructors are always created with a class, weather we write it ourselves or if class creates it for us(although it will not be visible ). Role of constructors is to initialize fields when we instantiate class. It is possible to create multiple constructors. Adding „: this()“ after parameters of constructor with parameters will execute default constructor before constructors with parameters. To easily create constructors type ctor keyword an new one will be generated for you.


If you have ever initialized class you have consumed constructors in one way or another. Example of consuming constructors would be in previous example where we have instantiated ClassWithConstructor class with string “Mike” and integer 1.


Is it possible to forbid class to initialize constructor?

Yes it is, just create empty one and set private access modifier.


What about other access modifiers? Can I use them all?

Public, private, protected, internal, or protected internal access modifiers are allowed to be used with constructors.


How can i initialize base class constructors ?

Simply type

after constructors parameters . Remember, you don’t have to supply parameters for base class constructor  because base constructor will always be called before derived class constructor, weather we call it or not.


Classes are your blueprints. Inside classes you place all your data types and logic. Class can be recognized by class keyword. Multiple classes can exist inside one namespace. So far you have probably heard of an object, object is an instance of an class, and many objects can be created from one class. As mentioned in previous post we know that class is an reference type.

How do we create class?


We said that „Inside classes you place all your data types and logic“. Ok… so how can we use it? First, we start by creating our custom class which will have an method that will print full name in console window, then we will call an instance of that class from our second class and supply method from there with parameters.

I hope you learned something from this article and that you will visit tomorrow when we will continue with operators.

© 2021 LearnC#WithDarko

Theme by Anders NorenUp ↑