Convert Stream to IEnumerable. If possible when "keeping laziness"

Posted by Binary255 on Stack Overflow See other posts from Stack Overflow or by Binary255
Published on 2010-04-13T14:32:55Z Indexed on 2010/04/13 15:23 UTC
Read the original article Hit count: 254

Filed under:

Hi,

I recieve a Stream and need to pass in a IEnumerable to another method.

public static void streamPairSwitchCipher(Stream someStream)
{
    ...
    someStreamAsIEnumerable = ...
    IEnumerable returned = anotherMethodWhichWantsAnIEnumerable(someStreamAsIEnumerable);
    ...
}

One way is to read the entire Stream, convert it to an Array of bytes and pass it in, as Array implements IEnumerable. But it would be much nicer if I could pass in it in such a way that I don't have to read the entire Stream before passing it in.

public static IEnumerable<T> anotherMethodWhichWantsAnIEnumerable<T>(IEnumerable<T> p) {
    ... // Something uninteresting
}

© Stack Overflow or respective owner

Related posts about c#