-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.CollectionsenhancementProduct code improvement that does NOT require public API changes/additionsProduct code improvement that does NOT require public API changes/additionsin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged
Milestone
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);
}
vertonghenb, julealgon, kronic and bart-vmware
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.CollectionsenhancementProduct code improvement that does NOT require public API changes/additionsProduct code improvement that does NOT require public API changes/additionsin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged