Virtualize component's Items property should be IReadOnlyCollection
See original GitHub issueBackground and Motivation
The <Virtualize TItem="T">
component should allow users to pass a IReadOnlyCollection<T>
to the Items
property/parameter instead of requiring an ICollection
.
There should be no need for the Virtualize
component to change the collection passed to it, so changing the parameter to an IReadOnlyCollection<T>
should be straight forward, that allows users to pass a wider range of collection types (including custom ones) to the Virtualize
component.
Proposed API change to Virtualize.cs
/// <summary>
/// Gets or sets the fixed item source.
/// </summary>
[Parameter]
-public ICollection<TItem>? Items { get; set; }
+public IReadOnlyCollection<TItem>? Items { get; set; }
Usage Examples
N/A
Alternative Designs
N/A
Risks
Since ICollection
does not implements IReadOnlyCollection
this is a breaking change if users are using custom collection types that implements ICollection
but not IReadOnlyCollection
. Changing that for the end user will be trivial though.
Updated: changed to reflect this is a breaking change. Op
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
c# - IEnumerable vs IReadonlyCollection vs ...
When an IEnumerable<T> is returned to the caller, they must consider the possibility that the returned object is a "lazy stream", e.g. a ......
Read more >The New Read-Only Collections in .NET 4.5
The IReadOnlyCollection is the most basic read-only collection interface and provides a Count property on top of its inherent IEnumerable ...
Read more >ASP.NET Core Razor component virtualization
Use the Virtualize component (reference source) when: Rendering a set of data items in a loop. Most of the items aren't visible due...
Read more >Exposing Private Collection Properties to Entity Framework
Ideally, collections should be exposed as either read-only collections or even simply as enumerations (IEnumerable ), with care taken to ensure ...
Read more >How to work with read-only collections in C# | InfoWorld
Collections enable you to allocate memory dynamically to store elements and then retrieve them using a key or index as needed.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@egil unfortunately we don’t do breaking changes like this unless we have a really good justification for them.
It’s unfortunate that we chose ICollection instead of IReadOnlyCollection but we prefer to avoid breaking existing customers (no matter how few they might be) over improving the correctness of the interface.
With changes like these, is not just about breaking an existing app that would need to recompile to work, its potentially breaking existing libraries that build on top and that would need to be updated.
That, we think is a much bigger risk compared to the benefit of being more “correct/explicit” about the type used.
I understand that this might not align with your preferences, but at least I hope the arguments explain our rationale.
I don’t think that this is currently the case: https://github.com/dotnet/runtime/blob/a14d27ac7ff3a58e39d889db72f7ce682c7492fd/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ICollection.cs#L8
See also https://github.com/dotnet/runtime/issues/31001