如何在 C# 中使用只读的 Collections
集合 表示一組可用于獲取和存儲的對象,在 C# 中提供了兩種類型的集合。
普通集合
泛型集合
前者存在于 System.Collections 命名空間下,屬類型不安全的,后者存在于 System.Collections.Generic 命名空間下,屬類型安全的。
不可變對象 定義為一旦創建就不可變更的對象, 在 .NET Core 中就存在著這三大 IReadOnlyList,IReadOnlyDictionary 和 IReadOnlyCollection 不可變集合,這篇文章我們就來討論這些不可變集合以及在C#中如何使用。
三大只讀類型介紹
IReadOnlyCollection 表示一個只讀集合的基礎接口,它實現了 IEnumerable 接口,代碼定義如下:
public?interface?IReadOnlyCollection<out?T>?:?IEnumerable<T>,?IEnumerable{int?Count?{?get;?}}IReadOnlyDictionary 表示一個字典的只讀形態,它實現了基礎的只讀集合接口 IReadOnlyCollection, 下面的代碼展示了如何將 泛型字典 只讀化。
public?IReadOnlyDictionary<string,?string>?Dictionary?{?get;?}?=?new?Dictionary<string,?string>{{?"1",?"ABC"?},{?"2",?"XYZ"?},{?"3",?"PQR"?},};IReadOnlyList 表示一個列表的只讀形態,值得注意的是 只讀集合 只能通過 index 訪問,如下代碼所示:
[DefaultMember("Item")]public?interface?IReadOnlyList<out?T>?:?IEnumerable<T>,?IEnumerable,?IReadOnlyCollection<T>{T?this[int?index]?{?get;?}}使用 IReadOnlyList 替換 List
接下來我們看一下如何使用 IReadOnlyList 替換 List 來實現列表的只讀化,考慮下面的類。
public?class?Author {public?int?Id?{?get;?set;?}public?string?FirstName?{?get;?set;?}public?string?LastName?{?get;?set;?} }假如你想從數據庫中返回 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"}}; }為了簡化,我省略了對數據庫繁瑣的操作,下面的代碼展示了如何在 Main 方法中調用 GetAuthors() 方法。
static?void?Main(string[]?args) {var?authors?=?GetAuthors();Console.Read();??????????? }顯而易見上面的這種 authors 集合是我們用的最多的可變集合,那現在的問題是如何阻止 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 接口
不知道大家可否發現,現存的只讀接口都繼承了 IEnumberable,這就意味著 IEnumberable 也是一種只讀形態,如果你只需要對集合進行迭代,那么就可以使用 IEnumberable 接口啦,如下代碼所示:
public?void?MyMethod(IEnumerable<Author>?authors) {foreach?(Author?author?in?authors){//Write?your?code?here} }如果需求不滿足,可以對 IEnumerable 繼續向下轉型,比如想對集合進行索引訪問,那么可以轉成 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 較早版本可用于只讀集合形態的接口, 在 .NET Core 中提供了新的只讀接口可用于阻止集合的修改,不過值得注意的是,這些對數據提供只讀視圖的接口,本質上來說也僅僅是高層的封裝而已。
譯文鏈接:https://www.infoworld.com/article/3610473/how-to-work-with-read-only-collections-in-csharp.html
總結
以上是生活随笔為你收集整理的如何在 C# 中使用只读的 Collections的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开源推荐:.Net Core3.1 +
- 下一篇: 如何从 dump 文件中提取出 C# 源