Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To answer the "why" part of the question as to why not <code>List&lt;T&gt;</code>, The reasons are future-proofing and API simplicity.</p> <p><strong>Future-proofing</strong></p> <p><code>List&lt;T&gt;</code> is not designed to be easily extensible by subclassing it; it is designed to be fast for internal implementations. You'll notice the methods on it are not virtual and so cannot be overridden, and there are no hooks into its <code>Add</code>/<code>Insert</code>/<code>Remove</code> operations. </p> <p>This means that if you need to alter the behaviour of the collection in the future (e.g. to reject null objects that people try to add, or to perform additional work when this happens such as updating your class state) then you need to change the type of collection you return to one you can subclass, which will be a breaking interface change (of course changing the semantics of things like not allowing null may also be an interface change, but things like updating your internal class state would not be).</p> <p>So by returning either a class that can be easily subclassed such as <code>Collection&lt;T&gt;</code> or an interface such as <code>IList&lt;T&gt;</code>, <code>ICollection&lt;T&gt;</code> or <code>IEnumerable&lt;T&gt;</code> you can change your internal implementation to be a different collection type to meet your needs, without breaking the code of consumers because it can still be returned as the type they are expecting.</p> <p><strong>API Simplicity</strong></p> <p><code>List&lt;T&gt;</code> contains a lot of useful operations such as <code>BinarySearch</code>, <code>Sort</code> and so on. However if this is a collection you are exposing then it is likely that you control the semantics of the list, and not the consumers. So while your class internally may need these operations it is very unlikely that consumers of your class would want to (or even should) call them.</p> <p>As such, by offering a simpler collection class or interface, you reduce the number of members that users of your API see, and make it easier for them to use.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload