'yield return works only for IEnumerable<T>?

Can I use yield return when the return type is an IGrouping<TKey, TElement> or an IDictionary<TKey, TValue>?



Solution 1:[1]

You could however return IEnumerable<KeyValuePair<K,V>> that would be similar to a dictionary. You would then yield return KeyValuePairs. You could even wrap this with another method that creates a dictionary out of the return. The only thing the first method would not guarantee is uniqueness in the keys.

Solution 2:[2]

Answer: No. A yield return statement can be used only if the return type is IEnumerator, IEnumerator<T>, IEnumerable, or IEnumerable<T>.

From §8.14 of the C# 3.0 spec:

The yield statement is used in an iterator block (§8.2) to yield a value to the enumerator object (§10.14.4) or enumerable object (§10.14.5) of an iterator or to signal the end of the iteration.

From §10.14.4:

An enumerator object has the following characteristics:

  1. It implements IEnumerator and IEnumerator<T>, where T is the yield type of the iterator.

[...]

From §10.14.5:

An enumerable object has the following characteristics:

  1. It implements IEnumerable and IEnumerable<T>, where T is the yield type of the iterator.

[...]

Solution 3:[3]

No, because an iterator block is simply a state machine built on your behalf by the compiler. This feature allows you to "yield" and item as a portion of a sequence.

If the return type was other than IEnumerable<T> (like IDictionary for example) the compiler would have to generate methods to implement that interface and at that point it wouldn't make much sense because you would be working with a collection rather than a sequence.

Solution 4:[4]

Just call the iterator method and chain ToDictionary or GroupBy after it, and you have much the same thing. Put that in a one-line wrapper method if you need to call it like that from several places.

Solution 5:[5]

I don't think so. While the documentation doesn't exactly spell it out, the way it is worded implies that it can only be used when the return type of the method is either IEnumerable or IEnumerable<T>. You might be able to write a class that implements IGrouping given an IEnumerable (that would be returned from your method using yield return), but that's about the only option really.

Solution 6:[6]

you can always create a IAsyncEnumerable<KeyValuePair<T,V>> using a repository or list and yield return new KeyValuePair<T,V>(obj1,obj2); and a second function to call the method above that returns 'IAsyncEnumerable<KeyValuePair<T,V>>' add into a dictionary and add to the dictionary if (!dic.TryGetValue(output.Key,out object s)) assuming you want unique keys

Solution 7:[7]

nope, just ienumerable :-)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Timothy Carter
Solution 2 jason
Solution 3 Andrew Hare
Solution 4 Daniel Earwicker
Solution 5 Andy
Solution 6 PontiacGTX
Solution 7 Joel Martinez