网页qq登陆网站,软件app制作,苏州网站建设的公司,提供邯郸手机网站建设集合 表示一组可用于获取和存储的对象#xff0c;在 C# 中提供了两种类型的集合。普通集合泛型集合前者存在于 System.Collections 命名空间下#xff0c;属类型不安全的#xff0c;后者存在于 System.Collections.Generic 命名空间下#xff0c;属类型安全的。不可变对象 … 集合 表示一组可用于获取和存储的对象在 C# 中提供了两种类型的集合。普通集合泛型集合前者存在于 System.Collections 命名空间下属类型不安全的后者存在于 System.Collections.Generic 命名空间下属类型安全的。不可变对象 定义为一旦创建就不可变更的对象, 在 .NET Core 中就存在着这三大 IReadOnlyList,IReadOnlyDictionary 和 IReadOnlyCollection 不可变集合这篇文章我们就来讨论这些不可变集合以及在C#中如何使用。三大只读类型介绍 IReadOnlyCollection 表示一个只读集合的基础接口它实现了 IEnumerable 接口代码定义如下public interface IReadOnlyCollectionout T : IEnumerableT, IEnumerable{int Count { get; }}IReadOnlyDictionary 表示一个字典的只读形态它实现了基础的只读集合接口 IReadOnlyCollection, 下面的代码展示了如何将 泛型字典 只读化。
public IReadOnlyDictionarystring, string Dictionary { get; } new Dictionarystring, string{{ 1, ABC },{ 2, XYZ },{ 3, PQR },};IReadOnlyList 表示一个列表的只读形态值得注意的是 只读集合 只能通过 index 访问如下代码所示[DefaultMember(Item)]public interface IReadOnlyListout T : IEnumerableT, IEnumerable, IReadOnlyCollectionT{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 ListAuthor GetAuthors()
{return new ListAuthor{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() 方法返回的集合不可更变做法就是将方法的返回值从 ListAuthor 修改为 IReadOnlyListAuthor如下代码所示。
public static IReadOnlyListAuthor GetAuthors()
{return new ListAuthor{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(IEnumerableAuthor authors)
{foreach (Author author in authors){//Write your code here}
}如果需求不满足可以对 IEnumerable 继续向下转型比如想对集合进行索引访问那么可以转成 IReadOnlyList 接口尽量满足 可用功能的最小化 改造后的代码如下
public void MyMethod(IReadOnlyListAuthor 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