-
Notifications
You must be signed in to change notification settings - Fork 11
FL0005
bagley2014 edited this page Oct 29, 2021
·
3 revisions
The ReadOnlyCollection<T>
returned by Libronix.Utility.EnumerableUtility.ToReadOnlyCollection
does not guarantee that a new read-only collection is created; it may just return a wrapper around an existing List<T>
.
The following pattern in a constructor is dangerous because it may save a reference to a List<T>
that a caller could modify:
public class Data
{
public Data(IEnumerable<T> values)
{
Values = values.ToReadOnlyCollection();
}
public ReadOnlyCollection<T> Values { get; }
}
- Use
.ToList().AsReadOnly()
to force a new, privateList
to be created. - Alternatively, change the argument to a
ReadOnlyCollection
orIReadOnlyList
in situations where avoiding copying the collection unnecessarily is worth the risk that the caller passes a wrapper around a collection it later mutates.
Constructions such as m_field = arg.Where(pred).Select(x => new { }).ToReadOnlyCollection();
will be flagged as violations of this rule, even though they clearly generate a private List
instance. Use .ToList().AsReadOnly()
in this situation to suppress the warning.