Sunday, 7 June 2015

How to create instance of an interface

In this article, i am going to show what is the way to create an instance of a interface. Actually, there is no direct way to create an instance of a interface. This is because they are incomplete (like templates) and creation of an object is completely meaningless here. If we want to do something like that, we must create an instance of class / type that implements that interface. For Example :- ILIST(T) interface provided in System.Collections.Generic namespace showing the below error while i was trying the same.


As i told you before, we must have to use it as a variable which points to a class which implements that interface to workaround this kind of problem. Something like that:-


Here, List(T) is a class which implementing ILIST(T) interface. Please remember that, ILIST(T) and ILIST are not same, the only difference is that first one is generic in type and we can pass any type in place of T. 

That’s it!!…..Happy Programming...