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

推荐订阅源

T
The Blog of Author Tim Ferriss
I
InfoQ
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
有赞技术团队
有赞技术团队
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
博客园_首页
Y
Y Combinator Blog
V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
雷峰网
雷峰网
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Help Net Security
P
Proofpoint News Feed
B
Blog
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - 青玄鸟

.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 官方文档