Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it common (or encouraged) practice to overload a function to accept IEnumerable<T>, ICollection<T>, IList<T>, etc.?
    text
    copied!<p><strong>EDIT</strong>:</p> <p>From the answers given, it's been made rather clear to me how the design I'm asking about below should actually be implemented. With those suggestions in mind (and in response to a comment politely pointing out that my example code does not even <em>compile</em>), I've edited the following code to reflect what the general consensus seems to be. The question that remains may no longer make sense in light of the code, but I'm leaving it as it is for posterity.</p> <hr> <p>Suppose I have three overloads of a function, one taking <code>IEnumerable&lt;T&gt;</code>, one taking <code>ICollection&lt;T&gt;</code>, and one taking <code>IList&lt;T&gt;</code>, something like the following:</p> <pre><code>public static T GetMiddle&lt;T&gt;(IEnumerable&lt;T&gt; values) { IList&lt;T&gt; list = values as IList&lt;T&gt;; if (list != null) return GetMiddle(list); int count = GetCount&lt;T&gt;(values); T middle = default(T); int index = 0; foreach (T value in values) { if (index++ &gt;= count / 2) { middle = value; break; } } return middle; } private static T GetMiddle&lt;T&gt;(IList&lt;T&gt; values) { int middleIndex = values.Count / 2; return values[middleIndex]; } private static int GetCount&lt;T&gt;(IEnumerable&lt;T&gt; values) { // if values is actually an ICollection&lt;T&gt; (e.g., List&lt;T&gt;), // we can get the count quite cheaply ICollection&lt;T&gt; genericCollection = values as ICollection&lt;T&gt;; if (genericCollection != null) return genericCollection.Count; // same for ICollection (e.g., Queue&lt;T&gt;, Stack&lt;T&gt;) ICollection collection = values as ICollection; if (collection != null) return collection.Count; // otherwise, we've got to count values ourselves int count = 0; foreach (T value in values) count++; return count; } </code></pre> <p>The idea here is that, if I've got an <code>IList&lt;T&gt;</code>, that makes my job easiest; on the other hand, I can still do the job with an <code>ICollection&lt;T&gt;</code> or even an <code>IEnumerable&lt;T&gt;</code>; the implementation for those interfaces just isn't as efficient.</p> <p>I wasn't sure if this would even work (if the runtime would be able to choose an overload based on the parameter passed), but I've tested it and it seems to.</p> <p>My question is: is there a problem with this approach that I haven't thought of? Alternately, is this in fact a good approach, but there's a better way of accomplishing it (maybe by attempting to cast the <code>values</code> argument up to an <code>IList&lt;T&gt;</code> first and running the more efficient overload if the cast works)? I'm just interested to know others' thoughts.</p>
 

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