By Svish


2009-08-05 09:24:15 8 Comments

Tried to run Run Code Analysis on a project here, and got a number of warnings that said something like this:

CA1002 : Microsoft.Design : Change 'List<SomeType>' in 'SomeClass.SomeProtectedOrPublicProperty' to use Collection, ReadOnlyCollection or KeyedCollection

Why should I use Collection<T> instead of List<T>? When I look at the msdn documentation, they seem almost equal. After reading the error help for the warning, I found that

System.Collections.Generic.List(T)_is a generic collection designed for performance not inheritance and, therefore, does not contain any virtual members.

But what does this really mean? And what should I be doing instead?

Should I keep using List<T> internally, and then in the properties return a new Collection<T>(someList) instead? Or should I just start using Collection<T> instead of List<T>?

2 comments

@Rob Levine 2009-08-05 09:30:20

In short, the generic list does not have virtual methods for Add, Remove etc, as it was designed to be fast, not extensible. This means that you cannot swap this concrete implementation out for a useful subclass (even though you can subclass it as it is not sealed).

Therefore, by exposing the List itself, you can never extend your collection to track add or remove operations (for example) without breaking the public contract of the class.

By exposing your collection as an IList or some-such, you can still use the List as the actual backing-store, but you retain future extensibility as you can swap out the concerete implementation later without changing the public contract of your class.

@Zoltán Tamási 2015-09-10 18:17:44

Just FYI, Collection<T> uses a List<T> instance internally.

@Simon P Stevens 2009-08-05 09:28:11

Collection exposes some virtual members (insert, remove, set, clear) that you can override and provide additional functionality (such as notification events) when the collection is changed.

You may not need this now, but it is a common requirement for classes that contain collections, so it's best to plan for it in advance. Because Collection is designed with extensibility in mind it's very flexible. If in the future you decide you need some extra feature in the collection you can just extend it without any change to the public interface of the class. If you had used a list, you would have had to change it to a collection which means it would have broken all of the callers of your class because they would have to be changed to use lists to.

List on the other hand is designed with performance in mind, so should only be used in specific cases where performance is very important. Because it is not extensible future changes to anything using a list will break everything else dependant on it. Normally List should only be used internally within very low level classes and not exposed to anything to reduce the chance of future breaking changes.

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] Why not inherit from List<T>?

32 Answered Questions

[SOLVED] What is the difference between a field and a property?

  • 2008-11-17 08:41:38
  • Anonymous
  • 453837 View
  • 1130 Score
  • 32 Answer
  • Tags:   c# properties field

64 Answered Questions

[SOLVED] What is the difference between String and string in C#?

15 Answered Questions

15 Answered Questions

[SOLVED] Calculate difference between two dates (number of days)?

  • 2009-10-22 13:47:15
  • leora
  • 1160743 View
  • 1092 Score
  • 15 Answer
  • Tags:   c# date

16 Answered Questions

[SOLVED] Difference Between Select and SelectMany

  • 2009-06-06 03:54:16
  • Tarik
  • 508506 View
  • 1073 Score
  • 16 Answer
  • Tags:   c# linq-to-sql linq

8 Answered Questions

35 Answered Questions

[SOLVED] Differences between HashMap and Hashtable?

32 Answered Questions

[SOLVED] What is the difference between const and readonly in C#?

Sponsored Content