Note that there are some explanatory texts on larger screens.

plurals
  1. POC# compiler not recognizing yield return methods as similar?
    primarykey
    data
    text
    <p>If I have two <code>yield return</code> methods with the same signature, the compiler does not seem to be recognizing them to be similar.</p> <p>I have two <code>yield return</code> methods like this:</p> <pre><code> public static IEnumerable&lt;int&gt; OddNumbers(int N) { for (int i = 0; i &lt; N; i++) if (i % 2 == 1) yield return i; } public static IEnumerable&lt;int&gt; EvenNumbers(int N) { for (int i = 0; i &lt; N; i++) if (i % 2 == 0) yield return i; } </code></pre> <p>With this, I would expect the following statement to compile fine:</p> <p><code>Func&lt;int, IEnumerable&lt;int&gt;&gt; generator = 1 == 0 ? EvenNumbers : OddNumbers; // Does not compile</code></p> <p>I get the error message </p> <blockquote> <p>Type of conditional expression cannot be determined because there is no implicit conversion between 'method group' and 'method group'</p> </blockquote> <p>However, an explicit cast works:</p> <p><code>Func&lt;int, IEnumerable&lt;int&gt;&gt; newGen = 1 == 0 ? (Func&lt;int, IEnumerable&lt;int&gt;&gt;)EvenNumbers : (Func&lt;int, IEnumerable&lt;int&gt;&gt;)OddNumbers; // Works fine</code></p> <p>Am I missing anything or Is this a bug in the C# compiler (I'm using VS2010SP1)?</p> <p>Note: I have read <a href="http://www.yoda.arachsys.com/csharp/csharp2/iterators.html" rel="nofollow">this</a> and still believe that the first one should've compiled fine.</p> <p>EDIT: Removed the usage of <code>var</code> in the code snippets as that wasn't what I intended to ask.</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.
 

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