1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        如何在 C# 中使用只讀的 Collections

        共 6184字,需瀏覽 13分鐘

         ·

        2021-03-25 17:14


        集合 表示一組可用于獲取和存儲的對象,在 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<stringstring> Dictionary { get; } = new Dictionary<stringstring>
                {
                    { "1""ABC" },
                    { "2""XYZ" },
                    { "3""PQR" },
                };

        IReadOnlyList 表示一個列表的只讀形態(tài),值得注意的是 只讀集合 只能通過 index 訪問,如下代碼所示:


            [DefaultMember("Item")]
            public interface IReadOnlyList<out T> : IEnumerable<T>, IEnumerableIReadOnlyCollection<T>
            {
                T this[int index] { get; }
            }

        使用 IReadOnlyList 替換 List

        接下來我們看一下如何使用 IReadOnlyList 替換 List 來實現(xiàn)列表的只讀化,考慮下面的類。


        public class Author
        {
           public int Id { getset; }
           public string FirstName { getset; }
           public string LastName { getset; }
        }

        假如你想從數(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ù)提供只讀視圖的接口,本質上來說也僅僅是高層的封裝而已。

        譯文鏈接:https://www.infoworld.com/article/3610473/how-to-work-with-read-only-collections-in-csharp.html



        往期精彩回顧




        【推薦】.NET Core開發(fā)實戰(zhàn)視頻課程 ★★★

        .NET Core實戰(zhàn)項目之CMS 第一章 入門篇-開篇及總體規(guī)劃

        【.NET Core微服務實戰(zhàn)-統(tǒng)一身份認證】開篇及目錄索引

        Redis基本使用及百億數(shù)據(jù)量中的使用技巧分享(附視頻地址及觀看指南)

        .NET Core中的一個接口多種實現(xiàn)的依賴注入與動態(tài)選擇看這篇就夠了

        10個小技巧助您寫出高性能的ASP.NET Core代碼

        用abp vNext快速開發(fā)Quartz.NET定時任務管理界面

        在ASP.NET Core中創(chuàng)建基于Quartz.NET托管服務輕松實現(xiàn)作業(yè)調度

        現(xiàn)身說法:實際業(yè)務出發(fā)分析百億數(shù)據(jù)量下的多表查詢優(yōu)化

        關于C#異步編程你應該了解的幾點建議

        C#異步編程看這篇就夠了


        瀏覽 94
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            国产又大又硬又爽免费视频试 | 美女操操 | 欧美伊人大香蕉 | 大香蕉伊人综合 | 性爱网站在线免费观看 | 大香蕉伊然在亚洲91 | 91久久国产综合久久91 | 女生扒开尿口让男生捅爽 | 亚洲日韩精品秘 在线观看 | 操逼视频网址 |