Question

As the title says can yield return values across multiple assemblies?

For instance assume the following solution exists:

SolutionA
    |
    |-Project A
    |
    |-Project B

If Project B is referenced by Project A, can a method in Project B yield values to a method in Project A?

Was it helpful?

Solution 2

Linq and its extension methods live in a different assembly than the one i use them from. So yes :) (edit: at least that was a very important reason to lean on the positive. Confirmation was pretty quick )

And confirmed just now between a console application assembly and a class library assembly :)

public IEnumerable<string> test()
{
     yield return "1";
     yield return "2";
     yield return "3";
     yield return "4";
}

in the class library is completely callable in the console application:

var t =new ClassLibrary1.Class1();
foreach (var test in t.test())
{
    Console.WriteLine(test);
}

OTHER TIPS

Iterator blocks (that is, methods that contain the yield keyword) are specific to that one method, in the sense that from anywhere outside of that method it is no different than any non-iterator block that returns an IEnumerable or IEnumerator. You can't, for example, yield a value to an iterator other than "yourself", so to speak. It's easier to show through code:

public class Foo
{
    public static IEnumerable<int> Bar()
    {
        yield return 1;
        Baz();
    }

    private static void Baz()
    {
        //there's no way for me to yield a second item out of Bar here
        //the best I can do is return an `IEnumerable` and have `Bar`
        //explicitly yield each of those items
    }
}

You can of course consume an iterator block from anywhere in which the method is accessible. You can call Bar from another method in the same class, from another class in the same assembly, or from another referenced assembly. To any of those callers it's no different than any other method returning an IEnumerable.

Why shouldn't it? From the outside, an iterator is just a method with the return type IEnumerable<T>, so if you're able to call the method, it will "yield return values across multiple assemblies".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top