如何在C#中使用只讀的 Collections
集合 表示一組可用于獲取和存儲的對象,在 C# 中提供了兩種類型的集合。
- 普通集合
- 泛型集合
前者存在于 System.Collections 命名空間下,屬類型不安全的,后者存在于 System.Collections.Generic 命名空間下,屬類型安全的。
不可變對象 定義為一旦創(chuàng)建就不可變更的對象, 在 .NET Core 中就存在著這三大 IReadOnlyList,IReadOnlyDictionary 和 IReadOnlyCollection 不可變集合,這篇文章我們就來討論這些不可變集合以及在C#中如何使用。
三大只讀類型介紹
IReadOnlyCollection 表示一個只讀集合的基礎接口,它實現(xiàn)了 IEnumerable 接口,代碼定義如下:
public interface IReadOnlyCollection<out T> : IEnumerable<T>, IEnumerable { int Count { get; } }
IReadOnlyDictionary 表示一個字典的只讀形態(tài),它實現(xiàn)了基礎的只讀集合接口 IReadOnlyCollection, 下面的代碼展示了如何將 泛型字典 只讀化。
public IReadOnlyDictionary<string, string> Dictionary { get; } = new Dictionary<string, string> { { "1", "ABC" }, { "2", "XYZ" }, { "3", "PQR" }, };
IReadOnlyList 表示一個列表的只讀形態(tài),值得注意的是 只讀集合 只能通過 index 訪問,如下代碼所示:
[DefaultMember("Item")] public interface IReadOnlyList<out T> : IEnumerable<T>, IEnumerable, IReadOnlyCollection<T> { T this[int index] { get; } }
使用 IReadOnlyList 替換 List
接下來我們看一下如何使用 IReadOnlyList 替換 List 來實現(xiàn)列表的只讀化,考慮下面的類。
public class Author { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
假如你想從數(shù)據(jù)庫中返回 author 集合,使用如下代碼:
public static List<Author> GetAuthors() { return new List<Author> { new Author { Id = 1, FirstName = "Joydip", LastName = "Kanjilal" }, new Author { Id = 2, FirstName = "Steve", LastName = "Smith" } }; }
為了簡化,我省略了對數(shù)據(jù)庫繁瑣的操作,下面的代碼展示了如何在 Main 方法中調用 GetAuthors() 方法。
static void Main(string[] args) { var authors = GetAuthors(); Console.Read(); }
顯而易見上面的這種 authors 集合是我們用的最多的可變集合,那現(xiàn)在的問題是如何阻止 authors 被修改呢?這里就可以使用 IReadOnlyList 來確保 GetAuthors() 方法返回的集合不可更變,做法就是將方法的返回值從 List<Author> 修改為 IReadOnlyList<Author>,如下代碼所示。
public static IReadOnlyList<Author> GetAuthors() { return new List<Author> { new Author { Id = 1, FirstName = "Joydip", LastName = "Kanjilal" }, new Author { Id = 2, FirstName = "Steve", LastName = "Smith" } }; }
接下來看一下 Main 下的 authors 是否有可添加的 Add() 方法?如下圖所示:
使用 IEnumberable 接口
不知道大家可否發(fā)現(xiàn),現(xiàn)存的只讀接口都繼承了 IEnumberable,這就意味著 IEnumberable 也是一種只讀形態(tài),如果你只需要對集合進行迭代,那么就可以使用 IEnumberable 接口啦,如下代碼所示:
public void MyMethod(IEnumerable<Author> authors) { foreach (Author author in authors) { //Write your code here } }
如果需求不滿足,可以對 IEnumerable 繼續(xù)向下轉型,比如想對集合進行索引訪問,那么可以轉成 IReadOnlyList 接口,盡量滿足 可用功能的最小化 ,改造后的代碼如下:
public void MyMethod(IReadOnlyList<Author> authors) { int count = authors.Count; for(int index = 0; index < count; index++) { var author = authors[index]; //Write your code here } }
IEnumerable 是 .NET 較早版本可用于只讀集合形態(tài)的接口, 在 .NET Core 中提供了新的只讀接口可用于阻止集合的修改,不過值得注意的是,這些對數(shù)據(jù)提供只讀視圖的接口,本質上來說也僅僅是高層的封裝而已。
以上就是如何在C#中使用只讀的 Collections的詳細內容,更多關于C#中使用只讀的 Collections的資料請關注腳本之家其它相關文章!
相關文章
C#中FormClosing與FormClosed的區(qū)別詳細解析
本文是對C#中FormClosing與FormClosed的區(qū)別進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-10-10C#實現(xiàn)ComboBox控件顯示出多個數(shù)據(jù)源屬性的方法
這篇文章主要介紹了C#實現(xiàn)ComboBox控件顯示出多個數(shù)據(jù)源屬性的方法,實例分析了ComboBox控件的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09C#使用zxing/zbar/thoughtworkQRcode解析二維碼的示例代碼
zxing是谷歌開源的二維碼庫,zbar,thoughtworkQRcode也是開源的,三者之間比較各有優(yōu)劣,本文將通過一個案例demo源碼,帶來認識學習下這三者的實際解碼效果,感興趣的可以了解一下2023-07-07Unity 按鈕事件封裝操作(EventTriggerListener)
這篇文章主要介紹了Unity 按鈕事件封裝操作(EventTriggerListener),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04C# 動態(tài)輸出Dos命令執(zhí)行結果的實例(附源碼)
這篇文章主要介紹了C# 動態(tài)輸出Dos命令執(zhí)行結果的實例,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下2020-07-07