Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# compiler not recognizing yield return methods as similar?

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.

like image 271
mherle Avatar asked Jul 22 '11 11:07

mherle


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

No. It is not a bug. It has nothing with yield. The thing is that expression type method group can be converted to delegate type only when it is assigned directly like: SomeDel d = SomeMeth.

C# 3.0 specification:

§6.6 Method group conversions

An implicit conversion (§6.1) exists from a method group (§7.1) to a compatible delegate type.

This is the only implicit conversion possible with method groups.

How ternary operator is evaluated in terms of types inferences:

A ? B : C:

Make sure that either B or C can be implicitly cast to one another's type. For example A ? 5 : 6.0 will be double because 5 can be implicitly cast to double. Type of A and B in this case is method group and there is no conversion between method group. Only to delegate and it can be enforced as you did.

like image 76
Andrey Avatar answered Oct 06 '22 23:10

Andrey