Friday, September 28, 2012

IList vs List

I will give very simple example

 Lets say we have a class library and we have class1 as class
 It has 2 methods
public IList getList()
{
 IList mylist=new List();
mylist.add("hello");
 mylist.add("hi"); return mylist;
 }

public List getList()
{
List mylist=new List();
mylist.add("hello");
mylist.add("hi");
return mylist;
 }

Now when the class library is being used. and it happens than you need to change the Collection from List to Array or indexed type.

We can do this.......
public IList getList()
{
IList mylist=new int[10];
 lst[0] = 10; return mylist;
 }
But We cannot do this.....
public List getList()
 {
 List mylist=new int[10];
lst[0] = 10; return mylist;
 }

No comments:

Post a Comment