Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This rule can indeed be noisy, but there are some very valid reasons to avoid <code>List&lt;T&gt;</code> in library code. It all depends on the context. Here are a few things to consider before disabling the rule or suppressing a given occurrence:</p> <ul> <li><p><code>List&lt;T&gt;</code> is often a poor choice for input parameters as it forces callers to copy data unnecessarily. I have seen lots of code that declares parameters as <code>List&lt;T&gt;</code> or <code>T[]</code> when <code>IEnumerable&lt;T&gt;</code> would suffice.</p></li> <li><p><code>List&lt;T&gt;</code> can be a poor choice for properties as well. Consider the following alternatives:</p> <pre><code>public class Course { public List&lt;Course&gt; Prerequisites { get; } } public class Course { public Collection&lt;Course&gt; Prerequisites { get; } } </code></pre> <p>The intention is that the caller can change a course's prerequistes by modifying the collection. In that case, if we use <code>List&lt;Course&gt;</code>, there is no way for the <code>Course</code> class to be notified when the prerequisites change since <code>List&lt;T&gt;</code> does not provide any modification callbacks. As such, using <code>List&lt;T&gt;</code> in this context is like having arbitrarily many public fields. On the other hand, we can subclass <code>Collection&lt;T&gt;</code> and override its virtuals to be notified of changes.</p></li> </ul> <p><code>List&lt;T&gt;</code> works best as a return value when complete ownership of the collection is transferred to the caller. This is why <code>Enumerable.ToList()</code> is actually perfectly reasonable and it does not violate the spirit of the rule.</p> <p>Now that I think about it, allowing <code>List&lt;T&gt;</code> as a return value from methods, but continuing to flag <code>List&lt;T&gt;</code> properties and parameters would probably greatly improve the rule's signal to noise ratio...</p>
    singulars
    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. 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