集合 (Collection) 是用來儲存和管理相關物件的群組,例如陣列(Array)、清單(List)等
而 集合介面 (Collection Interface)則是提供了一致的操作規範,使得各種不同的集合可以使用相同的方式操作。
在這裡列出在 C# 中常用的集合介面:
文章撰寫中...請稍後...
參考資料:
Microsoft.Learn - Collections
Microsoft.Learn - IEnumerable Interface
Microsoft.Learn - ICollection Interface
Microsoft.Learn - IList Interface
Microsoft.Learn - ISet<T> Interface
Microsoft.Learn - List<T> Class
而 集合介面 (Collection Interface)則是提供了一致的操作規範,使得各種不同的集合可以使用相同的方式操作。
在這裡列出在 C# 中常用的集合介面:
- IEnumerable:提供列舉器(Enumerator),可以使用 for 迴圈逐一歷遍
- ICollection:繼承 IEnumerable ,並提供了計數(Count)、同步(SyncRoot)和複製(CopyTo) 等方法
- IList:繼承 ICollection ,並增加了 新增(Add), 清除(Clear), 從指定位置新增(Insert) 等方法
- ISet:繼承 ICollection ,但集合中的元素是唯一的,不可重複
- IReadOnlyList:唯讀的 List
public List<string> GetData()
{
return new List<string> { "資料1", "資料2", "資料3" };
}
public IList<string> GetData()
{
return new List<string> { "資料1", "資料2", "資料3" };
}
文章撰寫中...請稍後...
參考資料:
Microsoft.Learn - Collections
Microsoft.Learn - IEnumerable Interface
Microsoft.Learn - ICollection Interface
Microsoft.Learn - IList Interface
Microsoft.Learn - ISet<T> Interface
Microsoft.Learn - List<T> Class
留言
張貼留言
如果有任何問題、建議、想說的話或文章題目推薦,都歡迎留言或來信: a@ruyut.com