Why can\'t we use both return and yield return in the same method?
For example, we can have GetIntegers1 and GetIntegers2 below, but not GetIntegers3.
pu
return
is eager. It returns the entire resultset at once. yield return
builds an enumerator. Behind the scenes the C# compiler emits the necessary class for the enumerator when you use yield return
. The compiler doesn't look for runtime conditions such as if ( someCondition )
when determining whether it should emit the code for an enumerable or have a method that returns a simple array. It detects that in your method you are using both which is not possible as he cannot emit the code for an enumerator and at the same time have the method return a normal array and all this for the same method.