Closed
Description
The List<>
class have a AsReadOnly
method to turn the collection into a ReadOnlyCollection
, but HashSet<>
does not have any such method.
Rationale and usage
Allows Entity Framework Core to expose a collection navigation property as a IReadOnlyCollection
when backing it with a private backing field declared as a HashSet
.
[Table("Blogs")]
public class Blog
{
private HashSet<Post> _posts;
public int BlogId { get; set; }
public IReadOnlyCollection<Post> Posts => _posts?.AsReadOnly();
public void AddPost(Post post)
{
// Do some business logic here...
_posts.Add(post);
}
}
Proposed API
namespace System.Collections.Generic;
public class CollectionExtensions
{
public ReadOnlyCollection<T> AsReadOnly(this IList<T> list);
public ReadOnlyDictionary<T> AsReadOnly(this IDictionary<T> dictionary);
+ public ReadOnlySet<T> AsReadOnly(this ISet<T> set);
}