CategoryInterface

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 IComparable interface

Class that implements IComparable interface comes with only one member, CompareTo method. CompareTo method compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.

In short we can compare two elements of the same collection and as a result we get int value that tells us relative order of the objects being compared.

We can also compare two instances of the same class and result tells us weather results are the same, first value is greater or less than compared value.

Example using non-generic IComparable interface:

Example using generic IComparable interface:

Difference between these two approaches:

  1. Using non-generic interface IComparable requires of us to cast obj to desired type.
  2. Generic interface IComparable<T> is type safe.

Explicit Interface member implementations

Explicit Interface are implemented explicitly if for example two or more of our interfaces implement same method/properties/etc… names and different implementation. Other usages can be to implement strong typing and cleaner code.

 

 

Implicit Interface member implementations

Example will show how to create new interface called ICarSpecifications which will hold a property and a method. ICarSpecifications interface will be implemented inside Car class and its implementation will be inside region InterfaceImplementation(note: you can manually create implementation OR when you type ICarSpecifications pres keys CTRL + . and click Implement missing members. Region will not be implemented automatically).

 

Tomorrow we will continue with explicit interfaces implementations.

Interfaces

Interfaces are a type of contract between interface and class or struct. Interface is defined by interface keyword and the name of interface should usually start with capital letter I to make it more recognizable.

 

When we say Interface is a contract, what do you really mean?

Every class or struct that uses some interface must implement all of its functionalities.
Why would you use Interface at all?

There are multiple reasons actually:

Firstly, a class or classes that use Interface must use all of Interfaces functionalities. Secondly, names of all properties, methods… must be same in all classes as they are in that specific Interface. Furthermore Interfaces are obligatory, imagine situation where you must in a few classes use 3 methods Get, Insert and Delete, when you use interface with your classes there is no possibility of forgetting to implement either of these methods. Lastly, because C# does not allow us to inherit multiple classes but allows us to implement multiple Interfaces, we can create multiple interfaces where each interface inherits a class and use these interface as workaround.

 

What can we define inside Interface?

Events

Indexers

Methods (but without implementation)

Properties

 

What are Interface limitations?

Interfaces cannot define:

Constants

Constructors

Fields

Operators

We cannot make interface or its members static

 

What about access modifiers?

By default all interface members are public and we cannot change it.

 

Can we instantiate Interface? Yes we can, limitation is that you must instantiate them using a class or struct that implements that interface.

 

Is there a limit to how many Interfaces can we implement at the same time? Yes and no. Although language specification state that you can implement as many Interfaces as you like, there are few technical limitations: heap size and metadata tables size, but worry not, you will never exceed that limit.

 

Please, drop by tomorrow to see a few Interface examples .

Explicit Interface member implementations

Implicit Interface member implementations

 

 

 

 

 

© 2019 LearnC#WithDarko

Theme by Anders NorenUp ↑