接口与类之将类的实例赋予接口的变量
public IList<CategoryInfo> GetCategories()
{
IList<CategoryInfo> categories = new List<CategoryInfo>();
//Execute a query to read the categories
using(SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CATEGORIES, null)) {
while (rdr.Read()) {
CategoryInfo cat = new CategoryInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2));
categories.Add(cat);
}
}
return categories;
}
最近在看PetShop,这段代码是项目SqlserverDAL中Category.cs,是用于接口实现的类。
现在不明白,IList<CategoryInfo> categories = new List<CategoryInfo>();
这是什么意思,把一个实例new List<CategoryInfo>()赋予接口变量IList<CategoryInfo> categories,对这个接口变量应该
怎么理解,它还是一个集合吗?应该怎么用,最近发现类的实例赋予接口的变量用得非常普遍,真的不是太明白。