C# compiler not recognizing yield return methods as similar?

Viewed 600

If I have two yield return methods with the same signature, the compiler does not seem to be recognizing them to be similar.

I have two yield return methods like this:

    public static IEnumerable<int> OddNumbers(int N)
    {
        for (int i = 0; i < N; i++)
            if (i % 2 == 1) yield return i;
    }
    public static IEnumerable<int> EvenNumbers(int N)
    {
        for (int i = 0; i < N; i++)
            if (i % 2 == 0) yield return i;
    }

With this, I would expect the following statement to compile fine:

Func<int, IEnumerable<int>> generator = 1 == 0 ? EvenNumbers : OddNumbers; // Does not compile

I get the error message

Type of conditional expression cannot be determined because there is no implicit conversion between 'method group' and 'method group'

However, an explicit cast works:

Func<int, IEnumerable<int>> newGen = 1 == 0 ? (Func<int, IEnumerable<int>>)EvenNumbers : (Func<int, IEnumerable<int>>)OddNumbers; // Works fine

Am I missing anything or Is this a bug in the C# compiler (I'm using VS2010SP1)?

Note: I have read this and still believe that the first one should've compiled fine.

EDIT: Removed the usage of var in the code snippets as that wasn't what I intended to ask.

8 Answers
Related