List<T>
to return collections from a public API. However, there are several reasons not to do so; instead return a ReadOnlyCollection<T>
. The are several benefits of
using a ReadOnlyCollection<T>
:
- The *collection itself* cannot be modified - i.e. you
cannot add, remove, or replace any item in the collection. So, it keeps the
collection itself intact. Note that a
ReadOnlyCollection<T>
does not mean that the elements within the collection are "readonly"; they can still be modified. To protect the items in it, you’d probably have to have private “setters” on any properties for the reference object in the collection – which is generally recommended. However, items that are declared as[DataMember]
must have both public getters and setters. So if you are in that case, you can’t do the private setter, but you can still use theReadOnlyCollection<T>
. Using this as a Design paradigm can prevent subtle bugs from popping up. One case that comes to mind is the case of aHashSet<T>
. It is recommended to avoid having mutable properties onclass
orstruct
types that are going to be used in any collection that utilizes the types hash code to refer to an object of that type - e.g.HashSet<T>
, - Performance: The
List<T>
class is essentially a wrapper around a strongly-typed array. Since arrays are immutable (cannot be re-sized), any modifications made to aList<T>
must create a complete copy of theList<T>
and add the new item. Note that in the case of value types, a copy of the value type is created whereas, in the case of reference types, a copy of the reference to the object is created. The performance impact for reference types is negligible. The initial capacity for aList<T>
is 4 unless specified thru the overloaded constructor - see here. https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.collectionsmarshal.getvaluereforadddefault Obviously, this can be very bad for memory and performance. Don’t forget that not only the items directly within theList<T>
itself are copied – but every value and object with the entire object graph for that reference type. This could easily contain other references types, collections, etc. This is why it is recommended to create/initialize aList<T>
instance with the overloaded constructor which takes anint
denoting the size of theList<T>
to be created when possible. This can often be done since you are typically iterating on a "known" size value at runtime. For example, creating aList<T>
of objects from a "Repository or DataService" method may iterate/read from aIDataReader
object which has a property ofRecordsAffected
. If you are going to be putting an item in theList<T>
based on the number of times theIDataReader
is read: e.g.while(reader.Read())
you can easily create the list like so:
if (reader != null && reader.RecordsAffected > 0)
{
// initialize the list with a pre-defined size
List<Foo> someList = new List<Foo>(reader.RecordsAffected);
while (reader.Read())
{
someList.Add(...);
...
}
}
Just as a side note, every time that a
List<T>
needs to grow in size (its new size would exceed its current Capactity
, the size is *doubled*. So,
if you happen to add just one more item to a List<T>
that wasn’t
constructed with a pre-determined size, and the List<T>
expands to
accommodate the new item, there will be a whole lot of unused space at the end
of the List<T>
even though there aren’t any items in it – bad for memory
and performance.
No comments:
Post a Comment