Declare variable as Interface vs. Class
-
Hi, Could anyone explain me the difference between interface and class variable. E.g.: ISomething ivar; //interface variable CSomething cvar; //class variable or IList<MyClass> var1; List<MyClass> var2; I am so confused the difference among them. Thanks in advance!
-
Hi, Could anyone explain me the difference between interface and class variable. E.g.: ISomething ivar; //interface variable CSomething cvar; //class variable or IList<MyClass> var1; List<MyClass> var2; I am so confused the difference among them. Thanks in advance!
Using an interface makes it simpler to use multiple varying class instances that implement an interface without the need for extra coding as the interface defines properties, methods and events that must be accessible.
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Hi, Could anyone explain me the difference between interface and class variable. E.g.: ISomething ivar; //interface variable CSomething cvar; //class variable or IList<MyClass> var1; List<MyClass> var2; I am so confused the difference among them. Thanks in advance!
Technically, there is little to no difference, you would need to instantiate both these variables. However, by defining
var1
as an interface type, you have left the actual definition open for later, giving additional flexibility. Based on some options, you could switch between different types at runtime when using anIList
. For e.g.if (flag)
{
var1 = new List();
}
else
{
var 1 = new MyOwnList(); //a custom implementation of IList.
}Build your own survey - http://www.factile.net
-
Hi, Could anyone explain me the difference between interface and class variable. E.g.: ISomething ivar; //interface variable CSomething cvar; //class variable or IList<MyClass> var1; List<MyClass> var2; I am so confused the difference among them. Thanks in advance!
Technically, there is little to no difference, you would need to instantiate both these variables. However, by defining
var1
as an interface type, you have left the actual definition open for later, giving additional flexibility. Based on some options, you could switch between different types at runtime when using anIList
. For e.g.if (flag)
{
var1 = new List();
}
else
{
var 1 = new MyOwnList(); //a custom implementation of the IList interface.
}Build your own survey - http://www.factile.net