petshop4.0中的一段代碼
public interface ICategory {
/// <summary>
/// Method to get all categories
/// </summary>
/// <returns>Interface to Model Collection Generic of categories</returns>
IList<CategoryInfo> GetCategories();
/// <summary>
/// Get information on a specific category
/// </summary>
/// <param name="categoryId">Unique identifier for a category</param>
/// <returns>Business Entity representing an category</returns>
CategoryInfo GetCategory(string categoryId);
}
我知道IList<CategoryInfo>是范型
表示list中的item是CategoryInfo對象
請問為什么用IList<CategoryInfo>
用List<CategoryInfo>可以嗎?
兩者有什么區(qū)別?謝謝
沒有什么區(qū)別,這樣寫靈活性大,實現(xiàn)ilist接口的類很多,你寫成list后,也許以后你要改成非list的,就會要改很多代碼
舉個例子
RenderControlToString(DataList L//Control C)
你認為是寫control還是寫datalist代碼的通用性高?
OOP編碼原則:盡可能用接口編程