惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog
罗磊的独立博客
S
SegmentFault 最新的问题
D
Docker
博客园 - 司徒正美
雷峰网
雷峰网
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
月光博客
月光博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
MongoDB | Blog
MongoDB | Blog

博客园 - 青玄鸟

.NET 中优雅处理 Server-Sent Events 请求取消 vue3.0 + ts 实现上传工厂(oss与cos) Dapr 订阅者参数无法正确反序列化问题 .NET 代码整洁手册 Blazor项目通过docker和nginx部署为静态站点的步骤 Moq mock 方法返回null空指针异常 基于接口隔离原则的依赖注入实现 HttpClient with Stream HttpClient partial update HttpClient 基本使用 值对象的封装 适配器模式 模板模式 最少知识原则 单例模式 抽象工厂 简单工厂、工厂方法、抽象工厂 工厂方法模式 使用 Visual Studio Code创建和执行T-SQL
只读集合类型属性实现
青玄鸟 · 2020-04-10 · via 博客园 - 青玄鸟

IReadOnlyCollection 接口

表示一个强类型的、只读的元素的集合

此接口没有定义集合操作的Add方法、索引器因此无法对集合元素进行增加和修改。

常见的List、Dictionary、Queue、Stack都实现了此接口

使用IReadOnlyCollection实现集合的只读

    public class ReadonlyCollectionDemo
    {
        private readonly List<int> _items = new List<int>();

        public IReadOnlyCollection<int> items => _items;

        public void AddItem(int item)
        {
            _items.Add(item);
        }

    }

以上只读集合类型属性的实现,体现了接口隔离原则:客户端不应该被强迫依赖它不需要的接口,客户端(调用者)针对IReadOnlyCollection接口编程,不会获取到它不应该依赖的Add方法、索引器等信息。

IReadOnlyCollection 官方文档