Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>IEnumerable&lt;object&gt;</code> is not a supertype of <code>IEnumerable&lt;T&gt;</code>, so it is not a supertype of <code>List&lt;T&gt;</code> either. See <a href="https://stackoverflow.com/questions/2575363/generics-list-extends-animal-is-same-as-listanimal">question 2575363</a> for a brief overview of why this is the case (it's about Java, but the concepts are the same). This problem has been solved in C# 4.0, by the way, which <a href="https://stackoverflow.com/questions/245607/how-is-generic-covariance-contra-variance-implemented-in-c-4-0">supports covariant generics</a>.</p> <p>The reason why you didn't find this error is because you used <code>x as T</code>, where you should have been using a normal cast (<code>(T)x</code>), see <a href="https://stackoverflow.com/questions/2139798/why-is-the-c-as-operator-so-popular">question 2139798</a>. The resulting <code>InvalidCastException</code> would have pointed you at your error. (In fact, if the type relationship were correct (i.e. if <code>IEnumerable&lt;object&gt;</code> were a supertype of <code>List&lt;T&gt;</code>), you wouldn't need a cast at all.)</p> <p>To solve your problem, make your method generic, so that it accepts an <code>IEnumerable&lt;T&gt;</code> instead of an <code>IEnumerable&lt;object&gt;</code>, and skip the cast completely.</p> <pre><code> public static void VerifyNotNullOrEmpty&lt;T&gt;(IEnumerable&lt;T&gt; theIEnumerable, string theIEnumerableName, string theVerifyingPosition) { ... } </code></pre>
 

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