You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ToReadOnlyCollection has somewhat unpredictable behaviour, depending on the nature of the IEnumerable<T> it's called on:
ReadOnlyCollection<T>: returns same object
List<T>: returns an .AsReadOnly() wrapper around it; changes the caller makes to the list will be reflected in this wrapper, which is usually undesirable behaviour when the return value of this call is stored in a field by a constructor
IEnumerable<T>: calls .ToList().AsReadOnly() and gets a new immutable list
Furthermore, this method returns ReadOnlyCollection<T>, which we would like to phase out.
Most usages of this method should be replaced with AsReadOnlyList(); some should change to .ToList().AsReadOnly().
N.B. enforcing this rule would be a huge breaking change. An intermediate step might be to enforce that .ToReadOnlyCollection() may not be used to initialize fields or properties in constructors and that .ToList().AsReadOnly() must be used instead. (This addresses the buggiest and least frequent usage.)
The text was updated successfully, but these errors were encountered:
Yes, phase it out in public APIs; there's nothing wrong with using it in an implementation to enforce read-only-ness. The suggestions above include using .ToList().AsReadOnly() which returns a ReadOnlyCollection<T>.
See Faithlife/FaithlifeUtility#5.
ToReadOnlyCollection
has somewhat unpredictable behaviour, depending on the nature of theIEnumerable<T>
it's called on:ReadOnlyCollection<T>
: returns same objectList<T>
: returns an.AsReadOnly()
wrapper around it; changes the caller makes to the list will be reflected in this wrapper, which is usually undesirable behaviour when the return value of this call is stored in a field by a constructorIEnumerable<T>
: calls.ToList().AsReadOnly()
and gets a new immutable listFurthermore, this method returns
ReadOnlyCollection<T>
, which we would like to phase out.Most usages of this method should be replaced with
AsReadOnlyList()
; some should change to.ToList().AsReadOnly()
.N.B. enforcing this rule would be a huge breaking change. An intermediate step might be to enforce that
.ToReadOnlyCollection()
may not be used to initialize fields or properties in constructors and that.ToList().AsReadOnly()
must be used instead. (This addresses the buggiest and least frequent usage.)The text was updated successfully, but these errors were encountered: